Interface JsonSerializerProvider

All Known Subinterfaces:
JsonProviders
All Known Implementing Classes:
JsonRegistries, JsonSerializerRegistry

public interface JsonSerializerProvider
Abstracts the run-time methods from the JsonSerializerRegistry so that serializers which have to look up dependencies don't depend on the full registry.
  • Method Summary

    Modifier and Type
    Method
    Description
    getSerializer(Class<T> clazz)
    Returns a registered serializer for the specified class, auto-generating it if necessary and possible.
    default com.google.gson.JsonElement
    Turns a value into a JsonElement.
    default Optional<com.google.gson.JsonElement>
    Turns a value into an optional JsonElement.
    boolean
    Checks whether the specified class is supported for serialization by any serializer that is known to this provider.
  • Method Details

    • supportsClassForSerialization

      boolean supportsClassForSerialization(Class<?> clazz)
      Checks whether the specified class is supported for serialization by any serializer that is known to this provider. Since this provider usually abstracts a JsonSerializerRegistry, this includes serializers that can be auto-generated on the fly.

      Note that there are some reasons why a registered serializer may claim to support a class, but fail at run-time when it actually encounters that class. Such a class will still be "supported" by that serializer from the point-of-view of this provider. Refer to the documentation of the individual serializers for details.

      Parameters:
      clazz - the class to check
      Returns:
      true if supported, false if not
    • getSerializer

      <T> JsonSerializer<T> getSerializer(Class<T> clazz) throws NotRegisteredException
      Returns a registered serializer for the specified class, auto-generating it if necessary and possible. This method will throw an exception if no serializer was registered manually that supports the class and no serializer can be auto-generated. If multiple serializers have been registered that can handle the specified class, the one registered later will take precedence.
      Type Parameters:
      T - the static type of the class to serialize
      Parameters:
      clazz - the class to return a serializer for
      Returns:
      the registered serializer, possibly auto-generated
      Throws:
      NotRegisteredException - if the class is not known to this provider
    • serialize

      default com.google.gson.JsonElement serialize(Object value) throws JsonSerializationException
      Turns a value into a JsonElement.
      Parameters:
      value - the value to convert
      Returns:
      the JSON element
      Throws:
      JsonSerializationException - if the value is in an inconsistent state or a state that cannot be turned into JSON, or is an instance of a class for which supportsClassForSerialization(Class) returns false, or is a value that requires the usage of serializeOptional(Object).
    • serializeOptional

      default Optional<com.google.gson.JsonElement> serializeOptional(Object value) throws JsonSerializationException
      Turns a value into an optional JsonElement. This method is meant to be called in a context in which values can vanish, such as object properties.
      Parameters:
      value - the value to convert
      Returns:
      the JSON element or nothing
      Throws:
      JsonSerializationException - if the value is in an inconsistent state or a state that cannot be turned into JSON, or is an instance of a class for which supportsClassForSerialization(Class) returns false.