Interface JsonbSerializer<T>

  • Type Parameters:
    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);
          }
     }
     
    Since:
    JSON Binding 1.0
    See Also:
    JsonbConfig, JsonbTypeSerializer, JsonbDeserializer, JsonbAdapter
    • Method Detail

      • serialize

        void serialize​(T obj,
                       jakarta.json.stream.JsonGenerator generator,
                       SerializationContext ctx)
        Serializes object into JSON stream.
        Parameters:
        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.