/
SerializationContexts.java
159 lines (137 loc) · 5.78 KB
/
SerializationContexts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.fasterxml.jackson.databind.cfg;
import com.fasterxml.jackson.core.TokenStreamFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import com.fasterxml.jackson.databind.ser.SerializerCache;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
/**
* Factory/builder class that replaces Jackson 2.x concept of "blueprint" instance
* of {@link SerializerProvider}. It will be constructed and configured during
* {@link ObjectMapper} building phase, and will be called once per {@code writeValue}
* call to construct actual stateful {@link SerializerProvider} to use during
* serialization.
*<p>
* Note that since this object has to be serializable (to allow JDK serialization of
* mapper instances), {@link SerializerProvider} need not be serializable any more.
*
* @since 3.0
*/
public abstract class SerializationContexts
implements java.io.Serializable
{
private static final long serialVersionUID = 3L;
/*
/**********************************************************************
/* Configuration
/**********************************************************************
*/
/**
* Low-level {@link TokenStreamFactory} that may be used for constructing
* embedded generators.
*/
final protected TokenStreamFactory _streamFactory;
/**
* Factory responsible for constructing standard serializers.
*/
final protected SerializerFactory _serializerFactory;
/**
* Cache for doing type-to-value-serializer lookups.
*/
final protected SerializerCache _serializerCache;
/*
/**********************************************************************
/* Life-cycle
/**********************************************************************
*/
protected SerializationContexts() { this(null, null, null); }
protected SerializationContexts(TokenStreamFactory tsf,
SerializerFactory serializerFactory, SerializerCache cache) {
_streamFactory = tsf;
_serializerFactory = serializerFactory;
_serializerCache = cache;
}
/**
* Mutant factory method called when instance is actually created for use by mapper
* (as opposed to coming into existence during building, module registration).
* Necessary usually to initialize non-configuration state, such as caching.
*/
public SerializationContexts forMapper(Object mapper,
TokenStreamFactory tsf, SerializerFactory serializerFactory) {
return forMapper(mapper, tsf, serializerFactory, _defaultCache());
}
protected abstract SerializationContexts forMapper(Object mapper,
TokenStreamFactory tsf, SerializerFactory serializerFactory,
SerializerCache cache);
/**
* Factory method for constructing context object for individual {@code
*/
public abstract DefaultSerializerProvider createContext(SerializationConfig config,
GeneratorSettings genSettings);
/*
/**********************************************************************
/* Overridable default methods
/**********************************************************************
*/
/**
* Factory method for constructing per-mapper serializer cache to use.
*/
protected SerializerCache _defaultCache() {
return new SerializerCache();
}
/*
/**********************************************************************
/* Access to caching details
/**********************************************************************
*/
/**
* Method that can be used to determine how many serializers this
* provider is caching currently
* (if it does caching: default implementation does)
* Exact count depends on what kind of serializers get cached;
* default implementation caches all serializers, including ones that
* are eagerly constructed (for optimal access speed)
*<p>
* The main use case for this method is to allow conditional flushing of
* serializer cache, if certain number of entries is reached.
*/
public int cachedSerializersCount() {
return _serializerCache.size();
}
/**
* Method that will drop all serializers currently cached by this provider.
* This can be used to remove memory usage (in case some serializers are
* only used once or so), or to force re-construction of serializers after
* configuration changes for mapper than owns the provider.
*/
public void flushCachedSerializers() {
_serializerCache.flush();
}
/*
/**********************************************************************
/* Vanilla implementation
/**********************************************************************
*/
public static class DefaultImpl extends SerializationContexts
{
private static final long serialVersionUID = 3L;
public DefaultImpl() { super(null, null, null); }
public DefaultImpl(TokenStreamFactory tsf,
SerializerFactory serializerFactory, SerializerCache cache) {
super(tsf, serializerFactory, cache);
}
@Override
public SerializationContexts forMapper(Object mapper,
TokenStreamFactory tsf, SerializerFactory serializerFactory,
SerializerCache cache) {
return new DefaultImpl(tsf, serializerFactory, cache);
}
@Override
public DefaultSerializerProvider createContext(SerializationConfig config,
GeneratorSettings genSettings) {
return new DefaultSerializerProvider.Impl(_streamFactory,
_serializerCache,
config, genSettings, _serializerFactory);
}
}
}