Class JsonEngine
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> Tdeserialize(com.google.gson.JsonElement source, com.google.gson.reflect.TypeToken<T> typeToken) deserializes JSON from aJsonElement.<T> Tdeserialize(com.google.gson.JsonElement source, Class<T> clazz) deserializes JSON from aJsonElement.deserialize(com.google.gson.JsonElement source, Type type) deserializes JSON from aJsonElement.<T> Tdeserialize(InputStream source, com.google.gson.reflect.TypeToken<T> typeToken) deserializes JSON from anInputStream.<T> Tdeserialize(InputStream source, Class<T> clazz) deserializes JSON from anInputStream.deserialize(InputStream source, Type type) deserializes JSON from anInputStream.<T> Tdeserialize(Reader source, com.google.gson.reflect.TypeToken<T> typeToken) deserializes JSON from anReader.<T> Tdeserialize(Reader source, Class<T> clazz) deserializes JSON from anReader.deserialize(Reader source, Type type) deserializes JSON from anReader.<T> Tdeserialize(String source, com.google.gson.reflect.TypeToken<T> typeToken) deserializes JSON from aString.<T> Tdeserialize(String source, Class<T> clazz) deserializes JSON from aString.deserialize(String source, Type type) deserializes JSON from aString.Getter method for the registry for deserializers.Getter method for the registries for converters.Getter method for the registry for serializers.voidregisterDeserializer(JsonDeserializer deserializer) Registers the specified deserializer.<T extends JsonSerializer<?> & JsonDeserializer>
voidregisterDualConverter(T converter) Registers the specified dual converter.voidregisterSerializer(JsonSerializer<?> serializer) Registers the specified serializer.voidseal()Seals this JSON engine, moving from the configuration phase to the run-time phase.serializeToString(Object value) Turns a value into a JSON string.booleansupportsClassForSerialization(Class<?> clazz) Checks whether the specified class is supported for serialization by this enginebooleanChecks whether the specified type is supported for deserialization by this enginecom.google.gson.JsonElementtoJsonElement(Object value) Turns a value into aJsonElement.voidwriteTo(Object value, OutputStream destination) Turns a value into JSON that is written to an output stream.voidTurns a value into JSON that is written to a writer.
-
Constructor Details
-
JsonEngine
public JsonEngine()Creates a new JSON engine with standard converters registered.
-
-
Method Details
-
registerSerializer
Registers the specified serializer.- Parameters:
serializer- the serializer to register
-
registerDeserializer
Registers the specified deserializer.- Parameters:
deserializer- the deserializer to register
-
registerDualConverter
Registers the specified dual converter.- Type Parameters:
T- the dual converter type which must implement bothJsonSerializerandJsonDeserializer- Parameters:
converter- the dual converter to register
-
getRegistries
Getter method for the registries for converters.- Returns:
- the registries
-
getSerializerRegistry
Getter method for the registry for serializers.- Returns:
- the registry for serializers
-
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
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
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
deserializes JSON from aString.- Type Parameters:
T- the static target type- Parameters:
source- the source stringclazz- 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 aString.- Type Parameters:
T- the static target type- Parameters:
source- the source stringtypeToken- 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
deserializes JSON from aString.- Parameters:
source- the source stringtype- 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
deserializes JSON from anInputStream. 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 streamclazz- 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 anInputStream. 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 streamtypeToken- 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
deserializes JSON from anInputStream. As demanded by the MIME type application/json, the input must be UTF-8 encoded.- Parameters:
source- the source streamtype- 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
deserializes JSON from anReader.- Type Parameters:
T- the static target type- Parameters:
source- the source readerclazz- 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 anReader.- Type Parameters:
T- the static target type- Parameters:
source- the source readertypeToken- 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
deserializes JSON from anReader.- Parameters:
source- the source readertype- 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 aJsonElement.- Type Parameters:
T- the static target type- Parameters:
source- the source elementclazz- 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 aJsonElement.- Type Parameters:
T- the static target type- Parameters:
source- the source elementtypeToken- 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 aJsonElement.- Parameters:
source- the source elementtype- the target type to deserialize to- Returns:
- the deserialized value
- Throws:
JsonDeserializationException- if the JSON does not match the target type
-
serializeToString
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
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 convertdestination- 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
Turns a value into JSON that is written to a writer.- Parameters:
value- the value to convertdestination- 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
Turns a value into aJsonElement.- 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
-