Package jakarta.json.bind.adapter
Interface JsonbAdapter<Original,Adapted>
- Type Parameters:
Original
- The type that JSONB doesn't know how to handleAdapted
- The type that JSONB knows how to handle out of the boxAdapter 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.
- Since:
- JSON Binding 1.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionadaptFromJson
(Adapted obj) This method is used on deserialization only.adaptToJson
(Original obj) This method is used on serialization only.
-
Method Details
-
adaptToJson
This method is used on serialization only. It contains a conversion logic from type Original to type Adapted. After conversion Adapted type will be mapped to JSON the standard way.- Parameters:
obj
- Object to convert ornull
.- Returns:
- Converted object which will be serialized to JSON or
null
. - Throws:
Exception
- if there is an error during the conversion.
-
adaptFromJson
This method is used on deserialization only. It contains a conversion logic from type Adapted to type Original.- Parameters:
obj
- Object to convert ornull
.- Returns:
- Converted object representing pojo to be set into object graph or
null
. - Throws:
Exception
- if there is an error during the conversion.
-