T
- Type to bind serializer for.public interface JsonbSerializer<T>
Interface representing a custom serializer for given type. Unlike JsonbAdapter
serializer provides more fine grained control over serialization process by writing java object directly into
JSON stream using JsonGenerator
. SerializationContext
acts as JSONB runtime, able to serialize
any java object provided.
Serializers are registered using JsonbConfig.withSerializers(JsonbSerializer[])
method or using JsonbTypeSerializer
annotation on type
Sample of custom Serializer:
class Box { public BoxInner boxInnerObject; public String name; } class BoxSerializer implements JsonbSerializer<Box> { public void serialize(Box box, JsonGenerator generator, SerializationContext ctx) { generator.write("name", box.name); ctx.serialize("boxInnerObject", generator); } }
JsonbConfig
,
JsonbTypeSerializer
,
JsonbDeserializer
,
JsonbAdapter
Modifier and Type | Method and Description |
---|---|
void |
serialize(T obj,
JsonGenerator generator,
SerializationContext ctx)
Serializes object into JSON stream.
|
void serialize(T obj, JsonGenerator generator, SerializationContext ctx)
obj
- Object to serialize.generator
- JSON generator used to write java object to JSON stream.ctx
- JSONB mapper context. Use it to serialize sub-objects.