T
- The target type of this serializer. It can be a super-class or interface, a concrete type
implementing a certain interface or class. When resolving the serializer, the one closest to
the object's type will be resolved. For example, if the object's type is a DMO (like Book),
and exists both a RecordSerializer
and a custom serializer for this Book DMO, then the
custom serializer will be resolved.public abstract class JavaTypeSerializer<T>
extends java.lang.Object
All public serializers defined in the com.goldencode.p2j.rest.serializers
package are resolved
and registered. Custom serializers can be specified by setting rest/custom-java-serializers
to a Java package holding these custom serializers, in the application's jar.
All mutable instances created by the serializer's fromJson(T, java.lang.String)
method can be expected to have their
changes reflected back on the caller, regardless if the argument is OUTPUT or not. It is the caller's
responsibility to ensure that only OUTPUT, INPUT-OUTPUT or RETURN arguments are being processed.
Modifier and Type | Field and Description |
---|---|
protected static JavaTypeSerializer |
BIGDECIMAL_SERIALIZER
The serializer for
BigDecimal instances. |
protected static JavaTypeSerializer |
BIGINTEGER_SERIALIZER
The serializer for
BigInteger instances. |
protected static JavaTypeSerializer |
BOOLEAN_SERIALIZER
The serializer for
Boolean instances. |
protected static JavaTypeSerializer |
BYTEARRAY_SERIALIZER
The serializer for Java byte arrays (byte[]).
|
protected static JavaTypeSerializer |
DOUBLE_SERIALIZER
The serializer for
Double instances. |
protected static JavaTypeSerializer |
FLOAT_SERIALIZER
The serializer for
Float instances. |
protected static JavaTypeSerializer |
INTEGER_SERIALIZER
The serializer for
Integer instances. |
protected static JavaTypeSerializer |
LONG_SERIALIZER
The serializer for
Long instances. |
protected java.lang.ThreadLocal<com.fasterxml.jackson.databind.ObjectMapper> |
mapper
Used to parse the JSON payload.
|
private static java.util.Map<java.lang.String,java.lang.Class<?>> |
PRIMITIVE_TYPES
The primitive type serializers.
|
private static java.util.Map<java.lang.String,JavaTypeSerializer> |
SERIALIZERS
The mapping of a type to its serializer.
|
protected static JavaTypeSerializer |
SHORT_SERIALIZER
The serializer for
Short instances. |
protected static JavaTypeSerializer |
STRING_SERIALIZER
The serializer for
String instances. |
private java.lang.Class<T> |
type
The serializer's type.
|
Constructor and Description |
---|
JavaTypeSerializer(java.lang.Class<T> type)
Initialize this serializer with the given type.
|
Modifier and Type | Method and Description |
---|---|
protected T |
fromJson(T arg,
com.fasterxml.jackson.databind.JsonNode node)
Parse the JSON node and assign it to the given argument.
|
abstract T |
fromJson(T arg,
java.lang.String sval)
Parse the string JSON representation and assign it to the given argument.
|
private static JavaTypeSerializer |
getSerializer(com.fasterxml.jackson.databind.JsonNode node)
Get a serializer based on the JSON node's type.
|
static JavaTypeSerializer |
getSerializer(java.lang.String stype)
Get the serializer associated with the given type.
|
static JavaTypeSerializer |
getSerializer(java.lang.String stype,
int extent)
Get the serializer associated with the given type.
|
private static JavaTypeSerializer |
getSerializerInt(java.lang.String stype,
int extent)
Get the serializer associated with the given type.
|
java.lang.Class<T> |
getType()
Get the serializer's type.
|
abstract T |
initialize(java.lang.Class<? extends T> definitionType)
Create an initial (default) instance for this serializer.
|
private static void |
loadSerializers(java.lang.String pkg)
Load all
JavaTypeSerializer implementation in the given package. |
protected T |
newInstance(java.lang.Class<? extends T> definitionType,
java.util.function.Supplier<? extends T> def)
Create a new instance of the given type.
|
protected Record |
newRecord(java.lang.Class<? extends DataModelObject> definitionType)
Create a new
DataModelObject of the given type. |
protected java.util.List<java.lang.Object> |
readArray(JavaTypeSerializer component,
java.lang.Class<?> ctype,
java.lang.String sval)
Read a JSON array and load it in a Java list.
|
protected void |
readArray(JavaTypeSerializer component,
java.lang.Class<?> ctype,
java.lang.String sval,
java.util.Collection target)
Given a JSON array represented as a string, load its values in a Java collection.
|
protected java.util.Collection |
readArray(JavaTypeSerializer component,
java.lang.String sval,
java.lang.Class<? extends java.util.Collection> type)
Given a JSON array represented as a string, load its values in a Java collection.
|
protected java.util.Map |
readMap(JavaTypeSerializer pkey,
JavaTypeSerializer pval,
java.lang.String sval,
java.lang.Class<? extends java.util.Map> type)
Given a JSON object represented as a string, load its keys and values in a map.
|
static java.lang.Class<?> |
resolveJavaType(java.lang.String stype)
Given a
Type.getTypeName() representation of a Java type, resolve it to a Java Class . |
abstract com.fasterxml.jackson.databind.JsonNode |
toJson(T val)
Serialize the given instance to JSON.
|
protected com.fasterxml.jackson.databind.JsonNode |
toJsonArray(java.util.Collection c,
JavaTypeSerializer component)
Create a JSON representation as an array, for the given collection.
|
protected com.fasterxml.jackson.databind.JsonNode |
toJsonObject(java.util.Map map,
JavaTypeSerializer pkey,
JavaTypeSerializer pval)
Create a JSON representation of the specified map.
|
protected static final JavaTypeSerializer STRING_SERIALIZER
String
instances.protected static final JavaTypeSerializer BOOLEAN_SERIALIZER
Boolean
instances.protected static final JavaTypeSerializer BIGDECIMAL_SERIALIZER
BigDecimal
instances.protected static final JavaTypeSerializer BIGINTEGER_SERIALIZER
BigInteger
instances.protected static final JavaTypeSerializer SHORT_SERIALIZER
Short
instances.protected static final JavaTypeSerializer INTEGER_SERIALIZER
Integer
instances.protected static final JavaTypeSerializer LONG_SERIALIZER
Long
instances.protected static final JavaTypeSerializer FLOAT_SERIALIZER
Float
instances.protected static final JavaTypeSerializer DOUBLE_SERIALIZER
Double
instances.protected static final JavaTypeSerializer BYTEARRAY_SERIALIZER
private static final java.util.Map<java.lang.String,JavaTypeSerializer> SERIALIZERS
Type.getTypeName()
.private static final java.util.Map<java.lang.String,java.lang.Class<?>> PRIMITIVE_TYPES
protected final java.lang.ThreadLocal<com.fasterxml.jackson.databind.ObjectMapper> mapper
private final java.lang.Class<T> type
public JavaTypeSerializer(java.lang.Class<T> type)
type
- The target type.public abstract T initialize(java.lang.Class<? extends T> definitionType) throws java.lang.InstantiationException, java.lang.IllegalAccessException
Can be null for non-mutable instances.
definitionType
- The type as it appears at the parameter's definition.null
for immutable types, a Java native value for native types or a new instance,
for mutable types.java.lang.InstantiationException
java.lang.IllegalAccessException
public abstract T fromJson(T arg, java.lang.String sval) throws RequestArgumentError
arg
- The argument (obtained via initialize(java.lang.Class<? extends T>)
.sval
- The string JSON representation of this argument.RequestArgumentError
- If the argument can't be parsed.public abstract com.fasterxml.jackson.databind.JsonNode toJson(T val)
val
- The instance to serialize.public static java.lang.Class<?> resolveJavaType(java.lang.String stype) throws java.lang.ClassNotFoundException
Type.getTypeName()
representation of a Java type, resolve it to a Java Class
.
If this is a native type name, then the PRIMITIVE_TYPES
will resolve it.
If there is a serializer registered for this type in SERIALIZERS
, the the serializer's
type
is returned.
Otherwise, it is assumed this is a Java type name, and will be resolved either by Class.forName(java.lang.String)
or via the AsmClassLoader
, in case of DMO types.
stype
- The string representation of this type.java.lang.ClassNotFoundException
- If the type can't be resolved.public static JavaTypeSerializer getSerializer(java.lang.String stype)
stype
- The string representation of this type (including any generic parameters), as returned by
Type.getTypeName()
.public static JavaTypeSerializer getSerializer(java.lang.String stype, int extent)
If the serializer is resolved to an ArraySerializer
, a new instance is created and the specified
extent is set.
stype
- The string representation of this type (including any generic parameters), as returned by
Type.getTypeName()
.extent
- In case of arrays, a positive value indicating the array's length.private static JavaTypeSerializer getSerializerInt(java.lang.String stype, int extent)
stype
- The string representation of this type (including any generic parameters), as returned by
Type.getTypeName()
.extent
- In case of arrays, a positive value indicating the array's length.private static JavaTypeSerializer getSerializer(com.fasterxml.jackson.databind.JsonNode node)
If one can't be resolved, null
is returned.
node
- The JSON node.private static void loadSerializers(java.lang.String pkg)
JavaTypeSerializer
implementation in the given package.
Only non-abstract and public classes are resolved.
pkg
- The package where the serializers reside.public java.lang.Class<T> getType()
protected T fromJson(T arg, com.fasterxml.jackson.databind.JsonNode node) throws RequestArgumentError
arg
- The argument (obtained via initialize(java.lang.Class<? extends T>)
.node
- The JSON node for this argument.RequestArgumentError
- If the argument can't be parsed.protected Record newRecord(java.lang.Class<? extends DataModelObject> definitionType) throws java.lang.InstantiationException, java.lang.IllegalAccessException
DataModelObject
of the given type.definitionType
- The DMO interface.Record
instance.java.lang.InstantiationException
java.lang.IllegalAccessException
protected T newInstance(java.lang.Class<? extends T> definitionType, java.util.function.Supplier<? extends T> def) throws java.lang.InstantiationException, java.lang.IllegalAccessException
definitionType
- The parameter's definition type.def
- The supplier of a default instance of this type.java.lang.InstantiationException
java.lang.IllegalAccessException
protected java.util.List<java.lang.Object> readArray(JavaTypeSerializer component, java.lang.Class<?> ctype, java.lang.String sval) throws RequestArgumentError
component
- The serializer for this array.ctype
- The component's type.sval
- The string JSON representation of this array.RequestArgumentError
- If the array could not be parsed.protected java.util.Collection readArray(JavaTypeSerializer component, java.lang.String sval, java.lang.Class<? extends java.util.Collection> type) throws RequestArgumentError
component
- The serializer for the array's values. If null
, it will be resolved from the
value's type.sval
- The string representation of this JSON array.type
- The collection's concrete type.
value's type.RequestArgumentError
- If the key or value can't be parsed.protected void readArray(JavaTypeSerializer component, java.lang.Class<?> ctype, java.lang.String sval, java.util.Collection target) throws RequestArgumentError
component
- The serializer for the array's values. If null
, it will be resolved from the
value's type.ctype
- The array's component concrete type. If null
it will be resolved from the
value's type.sval
- The string representation of this JSON array.target
- The target collection where to load the array.RequestArgumentError
- If the key or value can't be parsed.protected java.util.Map readMap(JavaTypeSerializer pkey, JavaTypeSerializer pval, java.lang.String sval, java.lang.Class<? extends java.util.Map> type) throws RequestArgumentError
pkey
- The serializer for the keys. If null
, it will be resolved from the keys's type.pval
- The serializer for the values. If null
, it will be resolved from the value's type.sval
- The string representation of this JSON object.type
- The map's concrete type.RequestArgumentError
- If the key or value can't be parsed.protected com.fasterxml.jackson.databind.JsonNode toJsonArray(java.util.Collection c, JavaTypeSerializer component)
c
- The collection.component
- The component's type. If null
, it will be resolved from the value's type.protected com.fasterxml.jackson.databind.JsonNode toJsonObject(java.util.Map map, JavaTypeSerializer pkey, JavaTypeSerializer pval)
map
- The map.pkey
- The key's serializer. This is not used, the keys are assumed as string.pval
- The value's serializer. If null
, it will be resolved from the value's type.