- All Implemented Interfaces:
Serializable,Snapshottable<SerializerCache>
Cache contains three kinds of entries, based on combination of class pair key. First class in key is for the type to serialize, and second one is type used for determining how to resolve value type. One (but not both) of entries can be null.
To prevent threads from observing partially-resolved serializers during
addAndResolveNonTypedSerializer(java.lang.Class<?>, tools.jackson.databind.ValueSerializer<java.lang.Object>, tools.jackson.databind.SerializationContext) calls, a two-phase write protocol is used:
newly constructed serializers are placed into _inProgressMap first, resolved
there (so cyclic POJO lookups can find the in-progress entry), and only moved to
_sharedMap after resolve() completes. Each entry is removed from
_inProgressMap as soon as it is promoted, so the map tends to stay empty
at steady state.
The lock-free read path (untypedValueSerializer(java.lang.Class<?>)) reads only from
_sharedMap, which therefore never contains a partially-resolved serializer.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intBy default, allow caching of up to 4000 serializer entries (for possibly up to 1000 types; but depending access patterns may be as few as half of that). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAndResolveNonTypedSerializer(Class<?> rawType, JavaType fullType, ValueSerializer<Object> ser, SerializationContext ctxt) Another alternative that will cover both access via raw type and matching fully resolved type, in one fell swoop.voidaddAndResolveNonTypedSerializer(Class<?> type, ValueSerializer<Object> ser, SerializationContext ctxt) voidaddAndResolveNonTypedSerializer(JavaType type, ValueSerializer<Object> ser, SerializationContext ctxt) voidaddTypedSerializer(Class<?> cls, ValueSerializer<Object> ser) voidaddTypedSerializer(JavaType type, ValueSerializer<Object> ser) Method called if none of lookups succeeded, and caller had to construct a serializer.voidflush()Method called by StdSerializationContext#flushCachedSerializers() to clear all cached serializersMethod that can be called to get a read-only instance populated from the most recent version of the shared lookup Map.protected Objectintsize()snapshot()typedValueSerializer(Class<?> cls) Method that checks if the shared (and hence, synchronized) lookup Map might have a typed serializer for given type.typedValueSerializer(JavaType type) Method that checks if the shared (and hence, synchronized) lookup Map might have a typed serializer for given type.untypedValueSerializer(Class<?> type) Returns the fully-resolved untyped serializer for the given type, ornullif not yet cached.Returns the fully-resolved untyped serializer for the given type, ornullif not yet cached.
-
Field Details
-
DEFAULT_MAX_CACHE_SIZE
public static final int DEFAULT_MAX_CACHE_SIZEBy default, allow caching of up to 4000 serializer entries (for possibly up to 1000 types; but depending access patterns may be as few as half of that).- See Also:
-
-
Constructor Details
-
SerializerCache
public SerializerCache() -
SerializerCache
public SerializerCache(int maxCached) - Since:
- 3.0
-
SerializerCache
-
SerializerCache
-
-
Method Details
-
readResolve
-
snapshot
- Specified by:
snapshotin interfaceSnapshottable<SerializerCache>
-
getReadOnlyLookupMap
Method that can be called to get a read-only instance populated from the most recent version of the shared lookup Map. -
size
public int size() -
untypedValueSerializer
Returns the fully-resolved untyped serializer for the given type, ornullif not yet cached. Reads from_sharedMapwhich only contains fully-resolved entries, so no lock is needed.During cyclic POJO resolution the resolving thread may re-enter this method before the in-progress serializer has been promoted to
_sharedMap. In that case, because the calling thread already holds the monitor (viasynchronized (this)inaddAndResolveNonTypedSerializer(java.lang.Class<?>, tools.jackson.databind.ValueSerializer<java.lang.Object>, tools.jackson.databind.SerializationContext)),Thread.holdsLock(java.lang.Object)istrueand we fall back to_inProgressMapto return the partially-resolved serializer to break the cycle. All other threads never hold the monitor and therefore exclusively see fully-resolved entries. -
untypedValueSerializer
Returns the fully-resolved untyped serializer for the given type, ornullif not yet cached. Reads from_sharedMapwhich only contains fully-resolved entries, so no lock is needed.During cyclic POJO resolution the resolving thread may re-enter this method before the in-progress serializer has been promoted to
_sharedMap. In that case, because the calling thread already holds the monitor (viasynchronized (this)inaddAndResolveNonTypedSerializer(java.lang.Class<?>, tools.jackson.databind.ValueSerializer<java.lang.Object>, tools.jackson.databind.SerializationContext)),Thread.holdsLock(java.lang.Object)istrueand we fall back to_inProgressMapto return the partially-resolved serializer to break the cycle. All other threads never hold the monitor and therefore exclusively see fully-resolved entries. -
typedValueSerializer
Method that checks if the shared (and hence, synchronized) lookup Map might have a typed serializer for given type. -
typedValueSerializer
Method that checks if the shared (and hence, synchronized) lookup Map might have a typed serializer for given type. -
addTypedSerializer
Method called if none of lookups succeeded, and caller had to construct a serializer. If so, we will update the shared lookup map so that it can be resolved via it next time. -
addTypedSerializer
-
addAndResolveNonTypedSerializer
public void addAndResolveNonTypedSerializer(Class<?> type, ValueSerializer<Object> ser, SerializationContext ctxt) -
addAndResolveNonTypedSerializer
public void addAndResolveNonTypedSerializer(JavaType type, ValueSerializer<Object> ser, SerializationContext ctxt) -
addAndResolveNonTypedSerializer
public void addAndResolveNonTypedSerializer(Class<?> rawType, JavaType fullType, ValueSerializer<Object> ser, SerializationContext ctxt) Another alternative that will cover both access via raw type and matching fully resolved type, in one fell swoop. -
flush
public void flush()Method called by StdSerializationContext#flushCachedSerializers() to clear all cached serializers
-