/
JsonViewSerializer.java
566 lines (509 loc) · 19 KB
/
JsonViewSerializer.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package com.monitorjbl.json;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URL;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.UUID;
import java.util.regex.Pattern;
public class JsonViewSerializer extends JsonSerializer<JsonView> {
/**
* Cached results from expensive (pure) methods
*/
private final Memoizer memoizer;
/**
* Map of custom serializers to take into account when serializing fields.
*/
private Map<Class<?>, JsonSerializer<Object>> customSerializersMap = null;
public JsonViewSerializer() {
this(1024);
}
public JsonViewSerializer(int maxCacheSize) {
this.memoizer = new Memoizer(maxCacheSize);
}
/**
* Registering custom serializer allows to the JSonView to deal with custom serializations for certains field types.<br>
* This way you could register for instance a JODA serialization as a DateTimeSerializer. <br>
* Thus, when JSonView find a field of that type (DateTime), it will delegate the serialization to the serializer specified.<br>
* Example:<br>
* <code>
* JsonViewSupportFactoryBean bean = new JsonViewSupportFactoryBean( mapper );
* bean.registerCustomSerializer( DateTime.class, new DateTimeSerializer() );
* </code>
*
* @param <T> Type class of the serializer
* @param cls {@link Class} The class type you want to add a custom serializer
* @param forType {@link JsonSerializer} The serializer you want to apply for that type
*/
@SuppressWarnings("unchecked")
public <T> void registerCustomSerializer(Class<T> cls, JsonSerializer<T> forType) {
if(customSerializersMap == null) {
customSerializersMap = new HashMap<>();
}
if(cls == null) {
throw new IllegalArgumentException("Class must not be null");
} else if(cls.equals(JsonView.class)) {
throw new IllegalArgumentException("Class cannot be " + JsonView.class);
} else if(customSerializersMap.containsKey(cls)) {
throw new IllegalArgumentException("Class " + cls + " already has a serializer registered (" + customSerializersMap.get(cls) + ")");
}
customSerializersMap.put(cls, (JsonSerializer<Object>) forType);
}
/**
* Unregister a previously registered serializer. @see registerCustomSerializer
*
* @param cls The class type the serializer was registered for
*/
public void unregisterCustomSerializer(Class<?> cls) {
if(customSerializersMap != null) {
customSerializersMap.remove(cls);
}
}
@Override
public void serialize(JsonView result, JsonGenerator jgen, SerializerProvider serializers) throws IOException {
new JsonWriter(serializers, jgen, result).write(null, result.getValue());
}
class JsonWriter {
Stack<String> path = new Stack<>();
String currentPath = "";
Match currentMatch = null;
Field referringField = null;
final SerializerProvider serializerProvider;
final JsonGenerator jgen;
final JsonView result;
JsonWriter(SerializerProvider serializerProvider, JsonGenerator jgen, JsonView result) {
this.serializerProvider = serializerProvider;
this.jgen = jgen;
this.result = result;
}
//internal use only to encapsulate what the current state was
private JsonWriter(JsonGenerator jgen, JsonView result, Match currentMatch, SerializerProvider serializerProvider) {
this.jgen = jgen;
this.result = result;
this.currentMatch = currentMatch;
this.serializerProvider = serializerProvider;
}
//internal use only to encapsulate what the current state was
private JsonWriter(JsonGenerator jgen, JsonView result, Match currentMatch,
String currentPath, Stack<String> path, Field referringField, SerializerProvider serializerProvider) {
this.jgen = jgen;
this.result = result;
this.currentMatch = currentMatch;
this.currentPath = currentPath;
this.referringField = referringField;
this.path = path;
this.serializerProvider = serializerProvider;
}
boolean writePrimitive(Object obj) throws IOException {
if(obj instanceof String) {
jgen.writeString((String) obj);
} else if(obj instanceof Integer) {
jgen.writeNumber((Integer) obj);
} else if(obj instanceof Long) {
jgen.writeNumber((Long) obj);
} else if(obj instanceof Short) {
jgen.writeNumber((Short) obj);
} else if(obj instanceof Double) {
jgen.writeNumber((Double) obj);
} else if(obj instanceof Float) {
jgen.writeNumber((Float) obj);
} else if(obj instanceof Character) {
jgen.writeNumber((Character) obj);
} else if(obj instanceof Byte) {
jgen.writeNumber((Byte) obj);
} else if(obj instanceof Boolean) {
jgen.writeBoolean((Boolean) obj);
} else if(obj == null) {
jgen.writeNull();
} else if(obj instanceof BigDecimal) {
jgen.writeNumber((BigDecimal) obj);
} else {
return false;
}
return true;
}
boolean writeSpecial(Object obj) throws IOException {
if(obj instanceof Date) {
serializerProvider.defaultSerializeDateValue((Date) obj, jgen);
} else if(obj instanceof Temporal) {
serializerProvider.defaultSerializeValue(obj, jgen);
} else if(obj instanceof URL) {
jgen.writeString(obj.toString());
} else if(obj instanceof URI) {
jgen.writeString(obj.toString());
} else if (obj instanceof UUID){
jgen.writeString(obj.toString());
} else if(obj instanceof Class) {
jgen.writeString(((Class) obj).getCanonicalName());
} else {
return false;
}
return true;
}
boolean writeEnum(Object obj) throws IOException {
if(obj.getClass().isEnum()) {
jgen.writeString(((Enum) obj).name());
} else {
return false;
}
return true;
}
@SuppressWarnings("unchecked")
boolean writeList(Object obj) throws IOException {
if(obj instanceof List || obj instanceof Set || obj.getClass().isArray()) {
Iterable iter;
if(obj.getClass().isArray()) {
if(obj instanceof byte[]) {
jgen.writeBinary((byte[]) obj);
return true;
} else {
iter = convertArray(obj);
}
} else {
iter = (Iterable<Object>) obj;
}
jgen.writeStartArray();
for(Object o : iter) {
new JsonWriter(jgen, result, currentMatch, currentPath, path, referringField, serializerProvider).write(null, o);
}
jgen.writeEndArray();
} else {
return false;
}
return true;
}
@SuppressWarnings("unchecked")
Iterable convertArray(Object obj) {
Iterable iter;
if(obj instanceof int[]) {
int[] arr = (int[]) obj;
iter = new ArrayList<>();
for(int v : arr) {
((List<Integer>) iter).add(v);
}
} else if(obj instanceof double[]) {
double[] arr = (double[]) obj;
iter = new ArrayList<>();
for(double v : arr) {
((List<Double>) iter).add(v);
}
} else if(obj instanceof float[]) {
float[] arr = (float[]) obj;
iter = new ArrayList<>();
for(float v : arr) {
((List<Float>) iter).add(v);
}
} else if(obj instanceof long[]) {
long[] arr = (long[]) obj;
iter = new ArrayList<>();
for(long v : arr) {
((List<Long>) iter).add(v);
}
} else if(obj instanceof short[]) {
short[] arr = (short[]) obj;
iter = new ArrayList<>();
for(short v : arr) {
((List<Short>) iter).add(v);
}
} else if(obj instanceof char[]) {
char[] arr = (char[]) obj;
iter = new ArrayList<>();
for(char v : arr) {
((List<Character>) iter).add(v);
}
} else if(obj instanceof boolean[]) {
boolean[] arr = (boolean[]) obj;
iter = new ArrayList<>();
for(boolean v : arr) {
((List<Boolean>) iter).add(v);
}
} else {
iter = Arrays.asList((Object[]) obj);
}
return iter;
}
@SuppressWarnings("unchecked")
boolean writeMap(Object obj) throws IOException {
if(obj instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) obj;
jgen.writeStartObject();
for(Object key : map.keySet()) {
jgen.writeFieldName(key.toString());
new JsonWriter(jgen, result, currentMatch, serializerProvider).write(null, map.get(key));
}
jgen.writeEndObject();
} else {
return false;
}
return true;
}
void writeObject(Object obj) throws IOException {
jgen.writeStartObject();
Class cls = obj.getClass();
while(!cls.equals(Object.class)) {
Field[] fields = getDeclaredFields(cls);
for(Field field : fields) {
try {
field.setAccessible(true);
Object val = field.get(obj);
if(valueAllowed(val, obj.getClass()) && fieldAllowed(field, obj.getClass())) {
String name = getFieldName(field);
jgen.writeFieldName(name);
if(customSerializersMap != null && val != null) {
JsonSerializer<Object> serializer = customSerializersMap.get(val.getClass());
if(serializer != null) {
serializer.serialize(val, jgen, serializerProvider);
} else {
new JsonWriter(jgen, result, currentMatch, currentPath, path, field, serializerProvider).write(name, val);
}
} else {
new JsonWriter(jgen, result, currentMatch, currentPath, path, field, serializerProvider).write(name, val);
}
}
} catch(IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
cls = cls.getSuperclass();
}
jgen.writeEndObject();
}
boolean valueAllowed(Object value, Class cls) {
return value != null
|| (serializerProvider.getConfig() != null
&& serializerProvider.getConfig().getSerializationInclusion() == Include.ALWAYS
&& getAnnotation(cls, JsonSerialize.class) == null)
|| (getAnnotation(cls, JsonSerialize.class) != null
&& readClassAnnotation(cls, JsonSerialize.class, "include") == Inclusion.ALWAYS);
}
@SuppressWarnings("unchecked")
boolean fieldAllowed(Field field, Class declaringClass) {
String name = field.getName();
String prefix = currentPath.length() > 0 ? currentPath + "." : "";
if(Modifier.isStatic(field.getModifiers())) {
return false;
}
//search for matching class
Match match = null;
Class cls = declaringClass;
while(!cls.equals(Object.class) && match == null) {
match = result.getMatch(cls);
//search for any matching interfaces as well, stopping on the first one
if(match == null && getInterfaces(cls) != null) {
for(Class iface : getInterfaces(cls)) {
match = result.getMatch(iface);
if(match != null) {
break;
}
}
}
cls = cls.getSuperclass();
}
if(match == null) {
match = currentMatch;
} else {
prefix = "";
}
//if there is a match, respect it
if(match != null) {
if(currentMatch == null) {
currentMatch = match;
}
int included = containsMatchingPattern(match.getIncludes(), prefix + name, true);
int excluded = containsMatchingPattern(match.getExcludes(), prefix + name, false);
/*
The logic for this is a little complex. We're dealing with ternary logic to
properly handle wildcard matches. We want matches made with wildcards to be
overruled by matches without them.
*/
if(included == 1) {
return true;
} else if(excluded == 1) {
return false;
} else if(included == 0) {
return true;
} else if(excluded == 0) {
return false;
} else {
return !annotatedWithIgnore(field);
}
} else {
//else, respect JsonIgnore only
return !annotatedWithIgnore(field);
}
}
void write(String fieldName, Object value) throws IOException {
if(fieldName != null) {
path.push(fieldName);
updateCurrentPath();
}
//try to handle all primitives/special cases before treating this as json object
if(!writePrimitive(value) && !writeSpecial(value) && !writeEnum(value) && !writeList(value) && !writeMap(value)) {
writeObject(value);
}
if(fieldName != null) {
path.pop();
updateCurrentPath();
}
}
void updateCurrentPath() {
StringBuilder builder = new StringBuilder();
for(String s : path) {
builder.append(".");
builder.append(s);
}
currentPath = builder.length() > 0 ? builder.toString().substring(1) : "";
}
@SuppressWarnings("unchecked")
<E> E readClassAnnotation(Class cls, Class annotationType, String methodName) {
try {
for(Annotation an : getAnnotations(cls)) {
Class<? extends Annotation> type = an.annotationType();
if(an.annotationType().equals(annotationType)) {
for(Method method : type.getDeclaredMethods()) {
if(method.getName().equals(methodName)) {
return (E) method.invoke(an, (Object[]) null);
}
}
throw new IllegalArgumentException("Method " + methodName + " not found on annotation " + annotationType);
}
}
throw new IllegalArgumentException("Annotation " + annotationType + " not found on class " + cls);
} catch(InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Returns one of the following values:
* <pre>
* -1: No match found
* 0: Wildcard-based match
* 1: Non-wildcard match
* </pre>
* <p>
* This method is memoized to speed up execution time
*/
int containsMatchingPattern(Set<String> values, String pattern, boolean matchPrefix) {
return memoizer.matches(values, pattern, matchPrefix, () -> {
int match = -1;
for(String val : values) {
String replaced = val.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*");
if(Pattern.compile(replaced).matcher(pattern).matches() || (matchPrefix && val.startsWith(pattern + "."))) {
match = replaced.contains("*") ? 0 : 1;
break;
}
}
return match;
});
}
/**
* Returns a boolean indicating whether the provided field is annotated with
* some form of ignore. This method is memoized to speed up execution time
*/
boolean annotatedWithIgnore(Field f) {
return memoizer.ignoreAnnotations(f, () -> {
JsonIgnore jsonIgnore = getAnnotation(f, JsonIgnore.class);
JsonIgnoreProperties classIgnoreProperties = getAnnotation(f.getDeclaringClass(), JsonIgnoreProperties.class);
JsonIgnoreProperties fieldIgnoreProperties = null;
boolean backReferenced = false;
//make sure the referring field didn't specify properties to ignore
if(referringField != null) {
fieldIgnoreProperties = getAnnotation(referringField, JsonIgnoreProperties.class);
}
//make sure the referring field didn't specify a backreference annotation
if(getAnnotation(f, JsonBackReference.class) != null && referringField != null) {
for(Field lastField : getDeclaredFields(referringField.getDeclaringClass())) {
JsonManagedReference fieldManagedReference = getAnnotation(lastField, JsonManagedReference.class);
if(fieldManagedReference != null && lastField.getType().equals(f.getDeclaringClass())) {
backReferenced = true;
break;
}
}
}
return (jsonIgnore != null && jsonIgnore.value()) ||
(classIgnoreProperties != null && Arrays.asList(classIgnoreProperties.value()).contains(f.getName())) ||
(fieldIgnoreProperties != null && Arrays.asList(fieldIgnoreProperties.value()).contains(f.getName())) ||
backReferenced;
});
}
private Class<?>[] getInterfaces(Class cls) {
return cls.getInterfaces();
}
private Field[] getDeclaredFields(Class cls) {
return cls.getDeclaredFields();
}
private Annotation[] getAnnotations(Class cls) {
return cls.getAnnotations();
}
private Annotation[] getAnnotations(Field field) {
return field.getAnnotations();
}
private String getFieldName(Field field) {
return memoizer.fieldName(field, () -> {
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
if(jsonProperty != null && jsonProperty.value().length() > 0) {
return jsonProperty.value();
} else {
return field.getName();
}
});
}
@SuppressWarnings("unchecked")
private <T extends Annotation> T getAnnotation(Class cls, Class<T> annotation) {
Annotation[] annotations = getAnnotations(cls);
if(annotations != null) {
for(Annotation a : annotations) {
if(a.annotationType().equals(annotation)) {
return (T) a;
}
}
}
return null;
}
@SuppressWarnings("unchecked")
private <T extends Annotation> T getAnnotation(Field field, Class<T> annotation) {
Annotation[] annotations = getAnnotations(field);
if(annotations != null) {
for(Annotation a : annotations) {
if(a.annotationType().equals(annotation)) {
return (T) a;
}
}
}
return null;
}
//synchronizes on the provided map to provide threadsafety
private <T, V> Map<T, V> fitToMaxSize(Map<T, V> map) {
synchronized (map) {
if(map.size() > 1) {
map.remove(map.keySet().iterator().next());
}
}
return map;
}
}
}