Class JsonEngine
- Direct Known Subclasses:
GsonBasedJsonEngine
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 StructuralJsonEngine.registerDualConverter(name.martingeisse.grumpyjson.serialize.JsonSerializer),
StructuralJsonEngine.registerSerializer(name.martingeisse.grumpyjson.serialize.JsonSerializer) and
StructuralJsonEngine.registerDeserializer(name.martingeisse.grumpyjson.deserialize.JsonDeserializer). If the standard
converters are not desired, you can call StructuralJsonEngine.getRegistries() / StructuralJsonEngine.getSerializerRegistry() /
StructuralJsonEngine.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.
This class is abstract because it delegates handling the actual JSON syntax to a JSON library such as Gson or Jackson. Concrete implementations implement the glue code for the various JSON libraries.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract <T> Tdeserialize(InputStream source, Class<T> clazz) deserializes JSON from anInputStream.abstract Objectdeserialize(InputStream source, Type type) deserializes JSON from anInputStream.abstract <T> Tdeserialize(InputStream source, TypeToken<T> typeToken) deserializes JSON from anInputStream.abstract <T> Tdeserialize(Reader source, Class<T> clazz) deserializes JSON from anReader.abstract Objectdeserialize(Reader source, Type type) deserializes JSON from anReader.abstract <T> Tdeserialize(Reader source, TypeToken<T> typeToken) deserializes JSON from anReader.abstract <T> Tdeserialize(String source, Class<T> clazz) deserializes JSON from aString.abstract Objectdeserialize(String source, Type type) deserializes JSON from aString.abstract <T> Tdeserialize(String source, TypeToken<T> typeToken) deserializes JSON from aString.abstract StringserializeToString(Object value) Turns a value into a JSON string.abstract voidwriteTo(Object value, OutputStream destination) Turns a value into JSON that is written to an output stream.abstract voidTurns a value into JSON that is written to a writer.Methods inherited from class name.martingeisse.grumpyjson.StructuralJsonEngine
deserialize, deserialize, deserialize, getDeserializerRegistry, getRegistries, getSerializerRegistry, registerDeserializer, registerDualConverter, registerSerializer, seal, supportsClassForSerialization, supportsTypeForDeserialization, toJsonElement
-
Constructor Details
-
JsonEngine
public JsonEngine()Creates a new JSON engine with standard converters registered.
-
-
Method Details
-
deserialize
public abstract <T> T deserialize(String source, Class<T> clazz) throws JsonDeserializationException 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 abstract <T> T deserialize(String source, 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
public abstract <T> T deserialize(InputStream source, Class<T> clazz) 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 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 abstract <T> T deserialize(InputStream source, 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
public abstract Object deserialize(InputStream source, Type type) throws JsonDeserializationException 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
public abstract <T> T deserialize(Reader source, Class<T> clazz) throws JsonDeserializationException 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 abstract <T> T deserialize(Reader source, 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
-
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
public abstract 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 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
-