Class JsonEngine

java.lang.Object
name.martingeisse.grumpyjson.JsonEngine

public final class JsonEngine extends Object
This class is the main entry point into the JSON conversion system.

This class should be used in two phases, configuration phase and runtime phase. The transition between the two phases is called sealing the engine. Simply put, first call configuration methods only, then seal the engine, then call runtime methods only. The behavior of this class is undefined if this sequence is not adhered to, and this class is expected to throw an exception in such a case.

A new instance of this class has standard converters for convenience. More converters can be added using registerDualConverter(name.martingeisse.grumpyjson.serialize.JsonSerializer), registerSerializer(name.martingeisse.grumpyjson.serialize.JsonSerializer) and registerDeserializer(name.martingeisse.grumpyjson.deserialize.JsonDeserializer). If the standard converters are not desired, you can call getRegistries() / getSerializerRegistry() / getDeserializerRegistry() and then .clear() to remove all currently registered converters. Note that you can override standard converters just by adding your own ones, since later-added converters will take precedence.

  • Constructor Details

    • JsonEngine

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

    • registerSerializer

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

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

      public <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 JsonRegistries getRegistries()
      Getter method for the registries for converters.
      Returns:
      the registries
    • getSerializerRegistry

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

      public 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 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 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 <T> T deserialize(String source, Class<T> clazz) throws JsonDeserializationException
      deserializes JSON from a String.
      Type Parameters:
      T - the static target type
      Parameters:
      source - the source string
      clazz - the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public <T> T deserialize(String source, com.google.gson.reflect.TypeToken<T> typeToken) throws JsonDeserializationException
      deserializes JSON from a String.
      Type Parameters:
      T - the static target type
      Parameters:
      source - the source string
      typeToken - a type token for the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public Object deserialize(String source, Type type) throws JsonDeserializationException
      deserializes JSON from a String.
      Parameters:
      source - the source string
      type - the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public <T> T deserialize(InputStream source, Class<T> clazz) throws JsonDeserializationException
      deserializes JSON from an InputStream. As demanded by the MIME type application/json, the input must be UTF-8 encoded.
      Type Parameters:
      T - the static target type
      Parameters:
      source - the source stream
      clazz - the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public <T> T deserialize(InputStream source, com.google.gson.reflect.TypeToken<T> typeToken) throws JsonDeserializationException
      deserializes JSON from an InputStream. As demanded by the MIME type application/json, the input must be UTF-8 encoded.
      Type Parameters:
      T - the static target type
      Parameters:
      source - the source stream
      typeToken - a type token for the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public Object deserialize(InputStream source, Type type) throws JsonDeserializationException
      deserializes JSON from an InputStream. As demanded by the MIME type application/json, the input must be UTF-8 encoded.
      Parameters:
      source - the source stream
      type - the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public <T> T deserialize(Reader source, Class<T> clazz) throws JsonDeserializationException
      deserializes JSON from an Reader.
      Type Parameters:
      T - the static target type
      Parameters:
      source - the source reader
      clazz - the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public <T> T deserialize(Reader source, com.google.gson.reflect.TypeToken<T> typeToken) throws JsonDeserializationException
      deserializes JSON from an Reader.
      Type Parameters:
      T - the static target type
      Parameters:
      source - the source reader
      typeToken - a type token for the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public Object deserialize(Reader source, Type type) throws JsonDeserializationException
      deserializes JSON from an Reader.
      Parameters:
      source - the source reader
      type - the target type to deserialize to
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON is malformed or does not match the target type
    • deserialize

      public <T> T deserialize(com.google.gson.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 <T> T deserialize(com.google.gson.JsonElement source, com.google.gson.reflect.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 Object deserialize(com.google.gson.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
    • serializeToString

      public String serializeToString(Object value) throws JsonSerializationException
      Turns a value into a JSON string.
      Parameters:
      value - the value to serialize
      Returns:
      the JSON string
      Throws:
      JsonSerializationException - if the value is in an inconsistent state or a state that cannot be turned into JSON
    • writeTo

      public void writeTo(Object value, OutputStream destination) throws JsonSerializationException
      Turns a value into JSON that is written to an output stream. As demanded by the MIME type application/json, the output will be UTF-8 encoded.
      Parameters:
      value - the value to convert
      destination - the stream to write to
      Throws:
      JsonSerializationException - if the value is in an inconsistent state or a state that cannot be turned into JSON
    • writeTo

      public void writeTo(Object value, Writer destination) throws JsonSerializationException
      Turns a value into JSON that is written to a writer.
      Parameters:
      value - the value to convert
      destination - the writer to write to
      Throws:
      JsonSerializationException - if the value is in an inconsistent state or a state that cannot be turned into JSON
    • toJsonElement

      public com.google.gson.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