-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathJsonNullableDeserializer.java
More file actions
98 lines (81 loc) · 3.5 KB
/
JsonNullableDeserializer.java
File metadata and controls
98 lines (81 loc) · 3.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
package org.openapitools.jackson.nullable;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.deser.std.ReferenceTypeDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.type.ReferenceType;
import java.io.IOException;
public class JsonNullableDeserializer extends ReferenceTypeDeserializer<JsonNullable<Object>> {
private static final long serialVersionUID = 1L;
private boolean isStringDeserializer = false;
private final boolean mapBlankStringToNull;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
public JsonNullableDeserializer(JavaType fullType, ValueInstantiator inst,
TypeDeserializer typeDeser, JsonDeserializer<?> deser,
boolean mapBlankStringToNull) {
super(fullType, inst, typeDeser, deser);
this.mapBlankStringToNull = mapBlankStringToNull;
if (fullType instanceof ReferenceType && ((ReferenceType) fullType).getReferencedType() != null) {
this.isStringDeserializer = ((ReferenceType) fullType).getReferencedType().isTypeOrSubTypeOf(String.class);
}
}
/*
/**********************************************************
/* Abstract method implementations
/**********************************************************
*/
@Override
public JsonNullable<Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonToken t = p.getCurrentToken();
if (t == JsonToken.VALUE_STRING && !isStringDeserializer) {
String str = p.getText().trim();
if (str.isEmpty()) {
return mapBlankStringToNull ? JsonNullable.of(null) : JsonNullable.undefined();
}
}
return super.deserialize(p, ctxt);
}
@Override
public JsonNullableDeserializer withResolved(TypeDeserializer typeDeser, JsonDeserializer<?> valueDeser) {
return new JsonNullableDeserializer(_fullType, _valueInstantiator,
typeDeser, valueDeser, mapBlankStringToNull);
}
@Override
public Object getAbsentValue(DeserializationContext ctxt) {
return JsonNullable.undefined();
}
@Override
public JsonNullable<Object> getNullValue(DeserializationContext ctxt) {
return JsonNullable.of(null);
}
@Override
public Object getEmptyValue(DeserializationContext ctxt) {
return JsonNullable.undefined();
}
@Override
public JsonNullable<Object> referenceValue(Object contents) {
return JsonNullable.of(contents);
}
@Override
public Object getReferenced(JsonNullable<Object> reference) {
return reference.get();
}
@Override
public JsonNullable<Object> updateReference(JsonNullable<Object> reference, Object contents) {
return JsonNullable.of(contents);
}
@Override
public Boolean supportsUpdate(DeserializationConfig config) {
// yes; regardless of value deserializer reference itself may be updated
return Boolean.TRUE;
}
}