Class StringConverter
- All Implemented Interfaces:
JsonDeserializer,JsonSerializer<String>
String.
This maps to and from JSON strings. No other mapping exists; in particular, JSON numbers are not converted to strings by this converter.
This converter is registered by default, and only needs to be manually registered if it gets removed, such as by
calling JsonRegistries.clear().
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondeserialize(com.google.gson.JsonElement json, Type type) Converts a value from JSON.com.google.gson.JsonElementConverts a value to JSON.booleansupportsClassForSerialization(Class<?> clazz) Checks if this serializer supports the specified class.booleanChecks if this deserializer supports the specified type.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface name.martingeisse.grumpyjson.deserialize.JsonDeserializer
deserializeAbsentMethods inherited from interface name.martingeisse.grumpyjson.serialize.JsonSerializer
serializeOptional
-
Constructor Details
-
StringConverter
public StringConverter()Constructor
-
-
Method Details
-
supportsTypeForDeserialization
Description copied from interface:JsonDeserializerChecks if this deserializer supports the specified type.- Specified by:
supportsTypeForDeserializationin interfaceJsonDeserializer- Parameters:
type- the type to check- Returns:
- true if supported, false if not
-
deserialize
public String deserialize(com.google.gson.JsonElement json, Type type) throws JsonDeserializationException Description copied from interface:JsonDeserializerConverts a value from JSON.This method must not be called with a type for which
JsonDeserializer.supportsTypeForDeserialization(Type)returns false. Calling it with such types anyway results in undefined behavior.- Specified by:
deserializein interfaceJsonDeserializer- Parameters:
json- the JSONtype- the type to deserialize- Returns:
- the deserialized value
- Throws:
JsonDeserializationException- if the JSON does not match the expected structure
-
supportsClassForSerialization
Description copied from interface:JsonSerializerChecks if this serializer supports the specified class.- Specified by:
supportsClassForSerializationin interfaceJsonSerializer<String>- Parameters:
clazz- the class to check- Returns:
- true if supported, false if not
-
serialize
Description copied from interface:JsonSerializerConverts a value to JSON.This method must not be called with values for whose class
JsonSerializer.supportsClassForSerialization(Class)returns false. Calling it with such values anyway results in undefined behavior.This method is not supported for values that can vanish during serialization, such as
OptionalField--JsonSerializer.serializeOptional(Object)should be called instead. If this method is called with such values anyway, it should always fail, even when the value does not vanish, to capture bugs early. Examples where this happens: - in a list of OptionalFields. While we could simply remove vanishing elements, doing so is just a weird way of filtering the list before serialization, which can be done the usual way. Moreover, there is no useful interpretation of a list-of-OptionalField during deserialization since all elements found in the JSON are known to be present, and vanishing elements cannot be found. - when turning a top-level OptionalField to JSON - when nesting two OptionalFields- Specified by:
serializein interfaceJsonSerializer<String>- Parameters:
value- the value to convert to JSON- Returns:
- the generated JSON
- Throws:
JsonSerializationException- if the value is in an inconsistent state, or in a state that cannot be converted to JSON
-