Original
- The type that JSONB doesn't know how to handleAdapted
- The type that JSONB knows how to handle out of the box
Adapter runtime "Original" and "Adapted" generic types are inferred from subclassing information, which is mandatory for adapter to work.
Sample 1:
// Generic information is provided by subclassing.
class BoxToCrateAdapter implements JsonbAdapter<Box<Integer>, Crate<String>> {...};
jsonbConfig.withAdapters(new BoxToCrateAdapter());
// Generic information is provided by subclassing with anonymous class
jsonbConfig.withAdapters(new JsonbAdapter<Box<Integer>, Crate<String>> {...});
Sample 2:
BoxToCrateAdapter<T> implements JsonbAdapter<Box<T>, Integer> {...};
// Bad way: Generic type information is lost due to type erasure
jsonbConfig.withAdapters(new BoxToCrateAdapter<Integer>());
// Proper way: Anonymous class holds generic type information
jsonbConfig.withAdapters(new BoxToCrateAdapter<Integer>(){});
public interface JsonbAdapter<Original,Adapted>
Allows to define custom mapping for given java type. The target type could be string or some mappable java type.
On serialization "Original" type is converted into "Adapted" type. After that "Adapted" type is serialized to JSON the standard way.
On deserialization it works the reverse way: JSON data are deserialized into "Adapted" type which is converted to "Original" type after that.
Adapters are registered using JsonbConfig.withAdapters(JsonbAdapter[])
method
or using JsonbTypeAdapter
annotation on class field.
JsonbConfig
,
JsonbTypeAdapter
Modifier and Type | Method and Description |
---|---|
Original |
adaptFromJson(Adapted obj)
This method is used on deserialization only.
|
Adapted |
adaptToJson(Original obj)
This method is used on serialization only.
|
Adapted adaptToJson(Original obj) throws java.lang.Exception
obj
- Object to convert or null
.null
.java.lang.Exception
- if there is an error during the conversion.Original adaptFromJson(Adapted obj) throws java.lang.Exception
obj
- Object to convert or null
.null
.java.lang.Exception
- if there is an error during the conversion.