Interface JsonSerializer<T>

Type Parameters:
T - the type this serializer handles
All Superinterfaces:
JsonComponent<T>
All Known Subinterfaces:
JsonConverter<T>

public interface JsonSerializer<T> extends JsonComponent<T>
Interface for serializing Java objects to JSON.

Implementations of this interface handle the conversion of Java objects of type T into their JSON representation using a JsonGenerator. Custom serializers can be registered to provide specialized serialization logic for specific types.

  • Method Summary

    Modifier and Type
    Method
    Description
    default boolean
    Checks if this serializer can be used for serializing map keys.
    void
    serialize(JsonGenerator generator, T instance, boolean writeNulls)
    Serializes the given instance to JSON using the provided generator.
    default String
    serializeAsMapKey(T instance)
    Serializes the given instance as a map key string.
    default void
    Serializes a null value.

    Methods inherited from interface io.helidon.json.binding.JsonComponent

    configure, type
  • Method Details

    • serialize

      void serialize(JsonGenerator generator, T instance, boolean writeNulls)
      Serializes the given instance to JSON using the provided generator.
      Parameters:
      generator - the JSON generator to write to
      instance - the object instance to serialize
      writeNulls - whether to write null values or skip them
    • serializeNull

      default void serializeNull(JsonGenerator generator)
      Serializes a null value.

      This method is called when a null value needs to be serialized. The default implementation writes a JSON null value.

      Parameters:
      generator - the JSON generator to write to
    • isMapKeySerializer

      default boolean isMapKeySerializer()
      Checks if this serializer can be used for serializing map keys.

      Some types may be suitable for use as map keys in JSON objects. The default implementation returns false.

      Method serializeAsMapKey(Object) should be implemented if this method should ever return true.
      Returns:
      true if this serializer can handle map key serialization, false otherwise
    • serializeAsMapKey

      default String serializeAsMapKey(T instance)
      Serializes the given instance as a map key string.

      This method should only be called if isMapKeySerializer() returns true. The default implementation throws an exception indicating the type is not supported.

      Parameters:
      instance - the object instance to serialize as a map key
      Returns:
      the string representation suitable for use as a JSON object key
      Throws:
      JsonException - if the type is not supported for map key serialization