Class JsonSerializerRegistry
- All Implemented Interfaces:
JsonSerializerProvider
JsonSerializers used by a JsonEngine.-
Constructor Summary
ConstructorsConstructorDescriptionJsonSerializerRegistry(RecordConverterFactory recordConverterFactory) NOT PUBLIC API -
Method Summary
Modifier and TypeMethodDescriptionprotected JsonSerializer<?>generateRegistrable(Class<?> clazz) Auto-generates a registrable in a subclass-specific way.protected StringgetErrorMessageForUnknownKey(Class<?> clazz) Produces an error message that gets used in an exception for an unknown key.<T> JsonSerializer<T>getSerializer(Class<T> clazz) Returns a registered serializer for the specified class, auto-generating it if necessary and possible.protected booleanregistrableSupports(JsonSerializer<?> registrable, Class<?> key) Checks whether the specified registrable supports the specified key.booleansupportsClassForSerialization(Class<?> clazz) Checks whether the specified class is supported for serialization by any serializer that is known to this provider.Methods inherited from class name.martingeisse.grumpyjson.registry.Registry
clear, get, onSeal, register, supportsMethods inherited from class name.martingeisse.grumpyjson.registry.Sealable
ensureConfigurationPhase, ensureRunTimePhase, sealMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface name.martingeisse.grumpyjson.serialize.JsonSerializerProvider
serialize, serializeOptional
-
Constructor Details
-
JsonSerializerRegistry
NOT PUBLIC API- Parameters:
recordConverterFactory- ...
-
-
Method Details
-
registrableSupports
Description copied from class:RegistryChecks whether the specified registrable supports the specified key.This method must always return the same result for the same registrable and key.
- Specified by:
registrableSupportsin classRegistry<Class<?>,JsonSerializer<?>> - Parameters:
registrable- the registrable which is checked for the keykey- the key to check- Returns:
- true if the registrable supports the key, false if not
-
generateRegistrable
Description copied from class:RegistryAuto-generates a registrable in a subclass-specific way. This method does not have to check the key for null, nor whether this registry is in the run-time phase.The returned registrable will only be used for the specified key, not for other keys that it might support as well. To re-use a single auto-generated registrable that supports multiple keys for all the keys it supports, the auto-generation itself must return the same registrable for all those keys.
This method must not cause calls to the registry while in progress. In particular, generating a registrable must not query the registry for other registrables to use as dependencies. There are two reasons for this: First, doing so may corrupt the state of this registry or cause deadlock -- the registry is not built to handle that case, simply because it is not needed by any known registrable. Second, doing so may easily cause infinite recursion when requesting the registrable that is already being built.
It is, however, allowed to build a registrable that refers to this registry when being used. Such a registrable can take the registry as a constructor parameter and store it internally -- and not access the registry inside its constructor to comply with the above rule -- and then access the registry at run-time. This is how various registrables act in practice. This turned out to be a more practical solution anyway because these registrables do not have all the required information available in the constructor, but only at run-time.
- Specified by:
generateRegistrablein classRegistry<Class<?>,JsonSerializer<?>> - Parameters:
clazz- the key (never null)- Returns:
- the auto-generated registrable, or null if auto-generation is not supported for that key
-
getErrorMessageForUnknownKey
Description copied from class:RegistryProduces an error message that gets used in an exception for an unknown key. The error message is one of the most frequent points of contact between the registry and application code, so it should be as developer-friendly as possible.- Specified by:
getErrorMessageForUnknownKeyin classRegistry<Class<?>,JsonSerializer<?>> - Parameters:
clazz- the unknown key- Returns:
- the error message
-
supportsClassForSerialization
Description copied from interface:JsonSerializerProviderChecks 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.
- Specified by:
supportsClassForSerializationin interfaceJsonSerializerProvider- Parameters:
clazz- the class to check- Returns:
- true if supported, false if not
-
getSerializer
Description copied from interface:JsonSerializerProviderReturns 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.- Specified by:
getSerializerin interfaceJsonSerializerProvider- 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
-