Top Description Fields Constructors Methods
com.fasterxml.jackson.databind.ser

public final Class SerializerCache

extends Object
Class Inheritance
Imports
java.util.concurrent.atomic.AtomicReference, com.fasterxml.jackson.databind.*, com.fasterxml.jackson.databind.ser.impl.ReadOnlyClassToSerializerMap, com.fasterxml.jackson.databind.util.LRUMap, .LookupCache, .TypeKey

Simple cache object that allows for doing 2-level lookups: first level is by "local" read-only lookup Map (used without locking) and second backup level is by a shared modifiable HashMap. The idea is that after a while, most serializers are found from the local Map (to optimize performance, reduce lock contention), but that during buildup we can use a shared map to reduce both number of distinct read-only maps constructed, and number of serializers constructed.

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.

Field Summary

Modifier and TypeField and Description
private final AtomicReference<ReadOnlyClassToSerializerMap>
_readOnlyMap

Most recent read-only instance, created from _sharedMap, if any.

private final LookupCache<TypeKey, JsonSerializer<Object>>
_sharedMap

Shared, modifiable map; all access needs to be through synchronized blocks.

public static final int
DEFAULT_MAX_CACHE_SIZE

By 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).

public static final int

Constructor Summary

AccessConstructor and Description
public
public
SerializerCache(int maxCached)

public

Method Summary

Modifier and TypeMethod and Description
private final synchronized ReadOnlyClassToSerializerMap
public void
public void
public void
addAndResolveNonTypedSerializer(Class<?> rawType, JavaType fullType, JsonSerializer<Object> ser, SerializerProvider provider)

Another alternative that will cover both access via raw type and matching fully resolved type, in one fell swoop.

public void
addTypedSerializer(JavaType type, JsonSerializer<Object> ser)

Method called if none of lookups succeeded, and caller had to construct a serializer.

public void
public synchronized void
flush()

Method called by StdSerializerProvider#flushCachedSerializers() to clear all cached serializers

public ReadOnlyClassToSerializerMap
getReadOnlyLookupMap()

Method that can be called to get a read-only instance populated from the most recent version of the shared lookup Map.

public synchronized int
size()

public JsonSerializer<Object>
public JsonSerializer<Object>
public JsonSerializer<Object>
untypedValueSerializer(Class<?> type)

Method that checks if the shared (and hence, synchronized) lookup Map might have untyped serializer for given type.

public JsonSerializer<Object>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_readOnlyMapback to summary
private final AtomicReference<ReadOnlyClassToSerializerMap> _readOnlyMap

Most recent read-only instance, created from _sharedMap, if any.

_sharedMapback to summary
private final LookupCache<TypeKey, JsonSerializer<Object>> _sharedMap

Shared, modifiable map; all access needs to be through synchronized blocks.

Note

keys are of various types (see below for key types), in addition to basic JavaType used for "untyped" serializers.

DEFAULT_MAX_CACHE_SIZEback to summary
public static final int DEFAULT_MAX_CACHE_SIZE

By 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).

DEFAULT_MAX_CACHEDback to summary
public static final int DEFAULT_MAX_CACHED

Deprecated

Use DEFAULT_MAX_CACHE_SIZE instead.

Annotations
@Deprecated

Constructor Detail

SerializerCacheback to summary
public SerializerCache()
SerializerCacheback to summary
public SerializerCache(int maxCached)
SerializerCacheback to summary
public SerializerCache(LookupCache<TypeKey, JsonSerializer<Object>> cache)
Since
2.16

Method Detail

_makeReadOnlyLookupMapback to summary
private final synchronized ReadOnlyClassToSerializerMap _makeReadOnlyLookupMap()
addAndResolveNonTypedSerializerback to summary
public void addAndResolveNonTypedSerializer(Class<?> type, JsonSerializer<Object> ser, SerializerProvider provider) throws JsonMappingException
addAndResolveNonTypedSerializerback to summary
public void addAndResolveNonTypedSerializer(JavaType type, JsonSerializer<Object> ser, SerializerProvider provider) throws JsonMappingException
addAndResolveNonTypedSerializerback to summary
public void addAndResolveNonTypedSerializer(Class<?> rawType, JavaType fullType, JsonSerializer<Object> ser, SerializerProvider provider) throws JsonMappingException

Another alternative that will cover both access via raw type and matching fully resolved type, in one fell swoop.

Since
2.7
addTypedSerializerback to summary
public void addTypedSerializer(JavaType type, JsonSerializer<Object> ser)

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.

addTypedSerializerback to summary
public void addTypedSerializer(Class<?> cls, JsonSerializer<Object> ser)
flushback to summary
public synchronized void flush()

Method called by StdSerializerProvider#flushCachedSerializers() to clear all cached serializers

getReadOnlyLookupMapback to summary
public ReadOnlyClassToSerializerMap getReadOnlyLookupMap()

Method that can be called to get a read-only instance populated from the most recent version of the shared lookup Map.

sizeback to summary
public synchronized int size()
typedValueSerializerback to summary
public JsonSerializer<Object> typedValueSerializer(JavaType type)
typedValueSerializerback to summary
public JsonSerializer<Object> typedValueSerializer(Class<?> cls)
untypedValueSerializerback to summary
public JsonSerializer<Object> untypedValueSerializer(Class<?> type)

Method that checks if the shared (and hence, synchronized) lookup Map might have untyped serializer for given type.

untypedValueSerializerback to summary
public JsonSerializer<Object> untypedValueSerializer(JavaType type)