forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsoniterSpi.java
More file actions
322 lines (263 loc) · 12.5 KB
/
JsoniterSpi.java
File metadata and controls
322 lines (263 loc) · 12.5 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package com.jsoniter.spi;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsoniterSpi {
// registered at startup, global state
private static Config defaultConfig;
private static List<Extension> extensions = new ArrayList<Extension>();
private static Map<Class, Class> typeImpls = new HashMap<Class, Class>();
private static Map<Type, Decoder> globalMapKeyDecoders = new HashMap<Type, Decoder>();
private static Map<Type, Encoder> globalMapKeyEncoders = new HashMap<Type, Encoder>();
private static Map<Type, Decoder> globalTypeDecoders = new HashMap<Type, Decoder>();
private static Map<Type, Encoder> globalTypeEncoders = new HashMap<Type, Encoder>();
private static Map<TypeProperty, Decoder> globalPropertyDecoders = new HashMap<TypeProperty, Decoder>();
private static Map<TypeProperty, Encoder> globalPropertyEncoders = new HashMap<TypeProperty, Encoder>();
// current state
private static ThreadLocal<Config> currentConfig = new ThreadLocal<Config>() {
@Override
protected Config initialValue() {
return defaultConfig;
}
};
private static volatile Map<Object, String> configNames = new HashMap<Object, String>();
private static volatile Map<String, Encoder> mapKeyEncoders = new HashMap<String, Encoder>();
private static volatile Map<String, Decoder> mapKeyDecoders = new HashMap<String, Decoder>();
private static volatile Map<String, Encoder> encoders = new HashMap<String, Encoder>();
private static volatile Map<String, Decoder> decoders = new HashMap<String, Decoder>();
private static volatile Map<Class, Extension> objectFactories = new HashMap<Class, Extension>();
static {
defaultConfig = Config.INSTANCE;
}
// === global ===
public static void setCurrentConfig(Config val) {
currentConfig.set(val);
}
public static void clearCurrentConfig() {
currentConfig.set(defaultConfig);
}
public static Config getCurrentConfig() {
return currentConfig.get();
}
public static void setDefaultConfig(Config val) {
defaultConfig = val;
}
public static Config getDefaultConfig() {
return defaultConfig;
}
public static String assignConfigName(Object obj) {
String configName = configNames.get(obj);
if (configName != null) {
return configName;
}
return assignNewConfigName(obj);
}
private synchronized static String assignNewConfigName(Object obj) {
String configName = configNames.get(obj);
if (configName != null) {
return configName;
}
long hash = obj.toString().hashCode();
if (hash < 0) {
hash = Long.MAX_VALUE + hash;
}
configName = "jsoniter_codegen.cfg" + hash + ".";
copyGlobalSettings(configName);
HashMap<Object, String> newCache = new HashMap<Object, String>(configNames);
newCache.put(obj, configName);
configNames = newCache;
return configName;
}
public static void registerExtension(Extension extension) {
if (!extensions.contains(extension)) {
extensions.add(extension);
}
}
// TODO: use composite pattern
public static List<Extension> getExtensions() {
ArrayList<Extension> combined = new ArrayList<Extension>(extensions);
combined.add(currentConfig.get());
return combined;
}
public static void registerMapKeyDecoder(Type mapKeyType, Decoder mapKeyDecoder) {
globalMapKeyDecoders.put(mapKeyType, mapKeyDecoder);
copyGlobalMapKeyDecoder(getCurrentConfig().configName(), mapKeyType, mapKeyDecoder);
}
public static void registerMapKeyEncoder(Type mapKeyType, Encoder mapKeyEncoder) {
globalMapKeyEncoders.put(mapKeyType, mapKeyEncoder);
copyGlobalMapKeyEncoder(getCurrentConfig().configName(), mapKeyType, mapKeyEncoder);
}
public static void registerTypeImplementation(Class superClazz, Class implClazz) {
typeImpls.put(superClazz, implClazz);
}
public static Class getTypeImplementation(Class superClazz) {
return typeImpls.get(superClazz);
}
public static void registerTypeDecoder(Class clazz, Decoder decoder) {
globalTypeDecoders.put(clazz, decoder);
copyGlobalTypeDecoder(getCurrentConfig().configName(), clazz, decoder);
}
public static void registerTypeDecoder(TypeLiteral typeLiteral, Decoder decoder) {
globalTypeDecoders.put(typeLiteral.getType(), decoder);
copyGlobalTypeDecoder(getCurrentConfig().configName(), typeLiteral.getType(), decoder);
}
public static void registerTypeEncoder(Class clazz, Encoder encoder) {
globalTypeEncoders.put(clazz, encoder);
copyGlobalTypeEncoder(getCurrentConfig().configName(), clazz, encoder);
}
public static void registerTypeEncoder(TypeLiteral typeLiteral, Encoder encoder) {
globalTypeEncoders.put(typeLiteral.getType(), encoder);
copyGlobalTypeEncoder(getCurrentConfig().configName(), typeLiteral.getType(), encoder);
}
public static void registerPropertyDecoder(Class clazz, String property, Decoder decoder) {
globalPropertyDecoders.put(new TypeProperty(clazz, property), decoder);
copyGlobalPropertyDecoder(getCurrentConfig().configName(), clazz, property, decoder);
}
public static void registerPropertyDecoder(TypeLiteral typeLiteral, String property, Decoder decoder) {
globalPropertyDecoders.put(new TypeProperty(typeLiteral.getType(), property), decoder);
copyGlobalPropertyDecoder(getCurrentConfig().configName(), typeLiteral.getType(), property, decoder);
}
public static void registerPropertyEncoder(Class clazz, String property, Encoder encoder) {
globalPropertyEncoders.put(new TypeProperty(clazz, property), encoder);
copyGlobalPropertyEncoder(getCurrentConfig().configName(), clazz, property, encoder);
}
public static void registerPropertyEncoder(TypeLiteral typeLiteral, String property, Encoder encoder) {
globalPropertyEncoders.put(new TypeProperty(typeLiteral.getType(), property), encoder);
copyGlobalPropertyEncoder(getCurrentConfig().configName(), typeLiteral.getType(), property, encoder);
}
// === copy from global to current ===
private static void copyGlobalSettings(String configName) {
for (Map.Entry<Type, Decoder> entry : globalMapKeyDecoders.entrySet()) {
copyGlobalMapKeyDecoder(configName, entry.getKey(), entry.getValue());
}
for (Map.Entry<Type, Encoder> entry : globalMapKeyEncoders.entrySet()) {
copyGlobalMapKeyEncoder(configName, entry.getKey(), entry.getValue());
}
for (Map.Entry<Type, Decoder> entry : globalTypeDecoders.entrySet()) {
copyGlobalTypeDecoder(configName, entry.getKey(), entry.getValue());
}
for (Map.Entry<Type, Encoder> entry : globalTypeEncoders.entrySet()) {
copyGlobalTypeEncoder(configName, entry.getKey(), entry.getValue());
}
for (Map.Entry<TypeProperty, Decoder> entry : globalPropertyDecoders.entrySet()) {
copyGlobalPropertyDecoder(configName, entry.getKey().type, entry.getKey().property, entry.getValue());
}
for (Map.Entry<TypeProperty, Encoder> entry : globalPropertyEncoders.entrySet()) {
copyGlobalPropertyEncoder(configName, entry.getKey().type, entry.getKey().property, entry.getValue());
}
}
private static void copyGlobalPropertyEncoder(String configName, Type type, String property, Encoder propertyEncoder) {
addNewEncoder(property + "@" + TypeLiteral.create(type).getEncoderCacheKey(), propertyEncoder);
}
private static void copyGlobalPropertyDecoder(String configName, Type type, String property, Decoder propertyDecoder) {
addNewDecoder(property + "@" + TypeLiteral.create(type).getDecoderCacheKey(), propertyDecoder);
}
private static void copyGlobalTypeEncoder(String configName, Type type, Encoder typeEncoder) {
addNewEncoder(TypeLiteral.create(type).getEncoderCacheKey(configName), typeEncoder);
}
private static void copyGlobalTypeDecoder(String configName, Type type, Decoder typeDecoder) {
addNewDecoder(TypeLiteral.create(type).getDecoderCacheKey(configName), typeDecoder);
}
private static void copyGlobalMapKeyDecoder(String configName, Type mapKeyType, Decoder mapKeyDecoder) {
addNewMapDecoder(TypeLiteral.create(mapKeyType).getDecoderCacheKey(configName), mapKeyDecoder);
}
private static void copyGlobalMapKeyEncoder(String configName, Type mapKeyType, Encoder mapKeyEncoder) {
addNewMapEncoder(TypeLiteral.create(mapKeyType).getEncoderCacheKey(configName), mapKeyEncoder);
}
public static String getMapKeyEncoderCacheKey(Type mapKeyType) {
TypeLiteral typeLiteral = TypeLiteral.create(mapKeyType);
return typeLiteral.getEncoderCacheKey();
}
public static String getMapKeyDecoderCacheKey(Type mapKeyType) {
TypeLiteral typeLiteral = TypeLiteral.create(mapKeyType);
return typeLiteral.getDecoderCacheKey();
}
// === current ===
public synchronized static void addNewMapDecoder(String cacheKey, Decoder mapKeyDecoder) {
HashMap<String, Decoder> newCache = new HashMap<String, Decoder>(mapKeyDecoders);
newCache.put(cacheKey, mapKeyDecoder);
mapKeyDecoders = newCache;
}
public static Decoder getMapKeyDecoder(String cacheKey) {
return mapKeyDecoders.get(cacheKey);
}
public synchronized static void addNewMapEncoder(String cacheKey, Encoder mapKeyEncoder) {
HashMap<String, Encoder> newCache = new HashMap<String, Encoder>(mapKeyEncoders);
newCache.put(cacheKey, mapKeyEncoder);
mapKeyEncoders = newCache;
}
public static Encoder getMapKeyEncoder(String cacheKey) {
return mapKeyEncoders.get(cacheKey);
}
public static Decoder getDecoder(String cacheKey) {
return decoders.get(cacheKey);
}
public synchronized static void addNewDecoder(String cacheKey, Decoder decoder) {
HashMap<String, Decoder> newCache = new HashMap<String, Decoder>(decoders);
if (decoder == null) {
newCache.remove(cacheKey);
} else {
newCache.put(cacheKey, decoder);
}
decoders = newCache;
}
public static Encoder getEncoder(String cacheKey) {
return encoders.get(cacheKey);
}
public synchronized static void addNewEncoder(String cacheKey, Encoder encoder) {
HashMap<String, Encoder> newCache = new HashMap<String, Encoder>(encoders);
if (encoder == null) {
newCache.remove(cacheKey);
} else {
newCache.put(cacheKey, encoder);
}
encoders = newCache;
}
public static boolean canCreate(Class clazz) {
if (objectFactories.containsKey(clazz)) {
return true;
}
for (Extension extension : getExtensions()) {
if (extension.canCreate(clazz)) {
addObjectFactory(clazz, extension);
return true;
}
}
return false;
}
public static Object create(Class clazz) {
return getObjectFactory(clazz).create(clazz);
}
public static Extension getObjectFactory(Class clazz) {
return objectFactories.get(clazz);
}
private synchronized static void addObjectFactory(Class clazz, Extension extension) {
HashMap<Class, Extension> copy = new HashMap<Class, Extension>(objectFactories);
copy.put(clazz, extension);
objectFactories = copy;
}
private static class TypeProperty {
public final Type type;
public final String property;
private TypeProperty(Type type, String property) {
this.type = type;
this.property = property;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TypeProperty that = (TypeProperty) o;
if (type != null ? !type.equals(that.type) : that.type != null) return false;
return property != null ? property.equals(that.property) : that.property == null;
}
@Override
public int hashCode() {
int result = type != null ? type.hashCode() : 0;
result = 31 * result + (property != null ? property.hashCode() : 0);
return result;
}
}
}