Class StructuralJsonEngine

java.lang.Object
name.martingeisse.grumpyjson.StructuralJsonEngine
Direct Known Subclasses:
JsonEngine

public class StructuralJsonEngine extends Object
This base class of JsonEngine implements the "conversion" part of the JSON engine and can be used if actual (de-)serialization from and to JSON syntax is not needed. In turn, it does not have any dependency on any concrete JSON syntax implementation such as Gson or Jackson, but operates purely on the structural JSON model defined in the json_model subpackage.

Please read the JsonEngine documentation for usage instructions.

  • Constructor Details

    • StructuralJsonEngine

      public StructuralJsonEngine()
      Creates a new JSON engine with standard converters registered.
  • Method Details

    • registerSerializer

      public final void registerSerializer(JsonSerializer<?> serializer)
      Registers the specified serializer.
      Parameters:
      serializer - the serializer to register
    • registerDeserializer

      public final void registerDeserializer(JsonDeserializer deserializer)
      Registers the specified deserializer.
      Parameters:
      deserializer - the deserializer to register
    • registerDualConverter

      public final <T extends JsonSerializer<?> & JsonDeserializer> void registerDualConverter(T converter)
      Registers the specified dual converter.
      Type Parameters:
      T - the dual converter type which must implement both JsonSerializer and JsonDeserializer
      Parameters:
      converter - the dual converter to register
    • getRegistries

      public final JsonRegistries getRegistries()
      Getter method for the registries for converters.
      Returns:
      the registries
    • getSerializerRegistry

      public final JsonSerializerRegistry getSerializerRegistry()
      Getter method for the registry for serializers.
      Returns:
      the registry for serializers
    • getDeserializerRegistry

      public final JsonDeserializerRegistry getDeserializerRegistry()
      Getter method for the registry for deserializers.
      Returns:
      the registry for deserializers
    • seal

      public void seal()
      Seals this JSON engine, moving from the configuration phase to the run-time phase.
    • supportsClassForSerialization

      public final boolean supportsClassForSerialization(Class<?> clazz)
      Checks whether the specified class is supported for serialization by this engine
      Parameters:
      clazz - the class to check
      Returns:
      true if supported, false if not
    • supportsTypeForDeserialization

      public final boolean supportsTypeForDeserialization(Type type)
      Checks whether the specified type is supported for deserialization by this engine
      Parameters:
      type - the type to check
      Returns:
      true if supported, false if not
    • deserialize

      public final <T> T deserialize(JsonElement source, Class<T> clazz) throws JsonDeserializationException
      deserializes JSON from a JsonElement.
      Type Parameters:
      T - the static target type
      Parameters:
      source - the source element
      clazz - the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON does not match the target type
    • deserialize

      public final <T> T deserialize(JsonElement source, TypeToken<T> typeToken) throws JsonDeserializationException
      deserializes JSON from a JsonElement.
      Type Parameters:
      T - the static target type
      Parameters:
      source - the source element
      typeToken - a type token for the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON does not match the target type
    • deserialize

      public final Object deserialize(JsonElement source, Type type) throws JsonDeserializationException
      deserializes JSON from a JsonElement.
      Parameters:
      source - the source element
      type - the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON does not match the target type
    • toJsonElement

      public final JsonElement toJsonElement(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