Class JsonSerializerRegistry

All Implemented Interfaces:
JsonSerializerProvider

public final class JsonSerializerRegistry extends Registry<Class<?>,JsonSerializer<?>> implements JsonSerializerProvider
This registry keeps the JsonSerializers used by a JsonEngine.
  • Constructor Details

    • JsonSerializerRegistry

      public JsonSerializerRegistry(RecordConverterFactory recordConverterFactory)
      NOT PUBLIC API
      Parameters:
      recordConverterFactory - ...
  • Method Details

    • registrableSupports

      protected boolean registrableSupports(JsonSerializer<?> registrable, Class<?> key)
      Description copied from class: Registry
      Checks whether the specified registrable supports the specified key.

      This method must always return the same result for the same registrable and key.

      Specified by:
      registrableSupports in class Registry<Class<?>,JsonSerializer<?>>
      Parameters:
      registrable - the registrable which is checked for the key
      key - the key to check
      Returns:
      true if the registrable supports the key, false if not
    • generateRegistrable

      protected JsonSerializer<?> generateRegistrable(Class<?> clazz)
      Description copied from class: Registry
      Auto-generates a registrable in a subclass-specific way. This method does not have to check the key for null, nor whether this registry is in the run-time phase.

      The returned registrable will only be used for the specified key, not for other keys that it might support as well. To re-use a single auto-generated registrable that supports multiple keys for all the keys it supports, the auto-generation itself must return the same registrable for all those keys.

      This method must not cause calls to the registry while in progress. In particular, generating a registrable must not query the registry for other registrables to use as dependencies. There are two reasons for this: First, doing so may corrupt the state of this registry or cause deadlock -- the registry is not built to handle that case, simply because it is not needed by any known registrable. Second, doing so may easily cause infinite recursion when requesting the registrable that is already being built.

      It is, however, allowed to build a registrable that refers to this registry when being used. Such a registrable can take the registry as a constructor parameter and store it internally -- and not access the registry inside its constructor to comply with the above rule -- and then access the registry at run-time. This is how various registrables act in practice. This turned out to be a more practical solution anyway because these registrables do not have all the required information available in the constructor, but only at run-time.

      Specified by:
      generateRegistrable in class Registry<Class<?>,JsonSerializer<?>>
      Parameters:
      clazz - the key (never null)
      Returns:
      the auto-generated registrable, or null if auto-generation is not supported for that key
    • getErrorMessageForUnknownKey

      protected String getErrorMessageForUnknownKey(Class<?> clazz)
      Description copied from class: Registry
      Produces an error message that gets used in an exception for an unknown key. The error message is one of the most frequent points of contact between the registry and application code, so it should be as developer-friendly as possible.
      Specified by:
      getErrorMessageForUnknownKey in class Registry<Class<?>,JsonSerializer<?>>
      Parameters:
      clazz - the unknown key
      Returns:
      the error message
    • supportsClassForSerialization

      public boolean supportsClassForSerialization(Class<?> clazz)
      Description copied from interface: JsonSerializerProvider
      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.

      Specified by:
      supportsClassForSerialization in interface JsonSerializerProvider
      Parameters:
      clazz - the class to check
      Returns:
      true if supported, false if not
    • getSerializer

      public <T> JsonSerializer<T> getSerializer(Class<T> clazz) throws NotRegisteredException
      Description copied from interface: JsonSerializerProvider
      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.
      Specified by:
      getSerializer in interface JsonSerializerProvider
      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