forked from OpenAPITools/jackson-databind-nullable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonNullableDeserializer.java
More file actions
94 lines (78 loc) · 3.23 KB
/
JsonNullableDeserializer.java
File metadata and controls
94 lines (78 loc) · 3.23 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
package org.openapitools.jackson.nullable;
import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonParser;
import tools.jackson.core.JsonToken;
import tools.jackson.databind.DeserializationConfig;
import tools.jackson.databind.DeserializationContext;
import tools.jackson.databind.JavaType;
import tools.jackson.databind.ValueDeserializer;
import tools.jackson.databind.deser.ValueInstantiator;
import tools.jackson.databind.deser.std.ReferenceTypeDeserializer;
import tools.jackson.databind.jsontype.TypeDeserializer;
import tools.jackson.databind.type.ReferenceType;
public class JsonNullableDeserializer extends ReferenceTypeDeserializer<JsonNullable<Object>> {
private static final long serialVersionUID = 1L;
private boolean isStringDeserializer = false;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
public JsonNullableDeserializer(JavaType fullType, ValueInstantiator inst,
TypeDeserializer typeDeser, ValueDeserializer<?> deser) {
super(fullType, inst, typeDeser, deser);
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 JacksonException {
JsonToken t = p.currentToken();
if (t == JsonToken.VALUE_STRING && !isStringDeserializer) {
String str = p.getString().trim();
if (str.isEmpty()) {
return JsonNullable.undefined();
}
}
return super.deserialize(p, ctxt);
}
@Override
protected ReferenceTypeDeserializer<JsonNullable<Object>> withResolved(TypeDeserializer typeDeser, ValueDeserializer<?> valueDeser) {
return new JsonNullableDeserializer(_fullType, _valueInstantiator,
typeDeser, valueDeser);
}
@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;
}
}