Interface JsonSerializerProvider
- All Known Subinterfaces:
JsonProviders
- All Known Implementing Classes:
JsonRegistries,JsonSerializerRegistry
public interface JsonSerializerProvider
Abstracts the run-time methods from the
JsonSerializerRegistry so that serializers which have to
look up dependencies don't depend on the full registry.-
Method Summary
Modifier and TypeMethodDescription<T> JsonSerializer<T>getSerializer(Class<T> clazz) Returns a registered serializer for the specified class, auto-generating it if necessary and possible.default com.google.gson.JsonElementTurns a value into aJsonElement.default Optional<com.google.gson.JsonElement>serializeOptional(Object value) Turns a value into an optionalJsonElement.booleansupportsClassForSerialization(Class<?> clazz) Checks whether the specified class is supported for serialization by any serializer that is known to this provider.
-
Method Details
-
supportsClassForSerialization
Checks whether the specified class is supported for serialization by any serializer that is known to this provider. Since this provider usually abstracts aJsonSerializerRegistry, this includes serializers that can be auto-generated on the fly.Note that there are some reasons why a registered serializer may claim to support a class, but fail at run-time when it actually encounters that class. Such a class will still be "supported" by that serializer from the point-of-view of this provider. Refer to the documentation of the individual serializers for details.
- Parameters:
clazz- the class to check- Returns:
- true if supported, false if not
-
getSerializer
Returns a registered serializer for the specified class, auto-generating it if necessary and possible. This method will throw an exception if no serializer was registered manually that supports the class and no serializer can be auto-generated. If multiple serializers have been registered that can handle the specified class, the one registered later will take precedence.- Type Parameters:
T- the static type of the class to serialize- Parameters:
clazz- the class to return a serializer for- Returns:
- the registered serializer, possibly auto-generated
- Throws:
NotRegisteredException- if the class is not known to this provider
-
serialize
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, or is an instance of a class for whichsupportsClassForSerialization(Class)returns false, or is a value that requires the usage ofserializeOptional(Object).
-
serializeOptional
default Optional<com.google.gson.JsonElement> serializeOptional(Object value) throws JsonSerializationException Turns a value into an optionalJsonElement. This method is meant to be called in a context in which values can vanish, such as object properties.- Parameters:
value- the value to convert- Returns:
- the JSON element or nothing
- Throws:
JsonSerializationException- if the value is in an inconsistent state or a state that cannot be turned into JSON, or is an instance of a class for whichsupportsClassForSerialization(Class)returns false.
-