Interface JsonbAdapter<Original,Adapted>

Type Parameters:
Original - The type that JSONB doesn't know how to handle
Adapted - 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.

Since:
JSON Binding 1.0
See Also: