5
5
import com .amazonaws .services .lambda .runtime .serialization .PojoSerializer ;
6
6
import com .fasterxml .jackson .core .JsonFactory ;
7
7
import com .fasterxml .jackson .core .JsonParser ;
8
+ import com .fasterxml .jackson .core .json .JsonReadFeature ;
9
+ import com .fasterxml .jackson .core .json .JsonWriteFeature ;
8
10
import com .fasterxml .jackson .core .JsonGenerator ;
9
11
import com .fasterxml .jackson .annotation .JsonInclude .Include ;
10
12
import com .fasterxml .jackson .databind .DeserializationConfig ;
19
21
import com .fasterxml .jackson .databind .PropertyNamingStrategy ;
20
22
import com .fasterxml .jackson .databind .SerializationConfig ;
21
23
import com .fasterxml .jackson .databind .SerializationFeature ;
24
+ import com .fasterxml .jackson .databind .json .JsonMapper ;
22
25
import com .fasterxml .jackson .databind .module .SimpleModule ;
26
+ import com .fasterxml .jackson .datatype .jdk8 .Jdk8Module ;
27
+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
23
28
24
29
import java .io .IOException ;
25
30
import java .io .OutputStream ;
@@ -50,69 +55,72 @@ public ObjectMapper getMapper() {
50
55
51
56
52
57
private static ObjectMapper createObjectMapper () {
53
- ObjectMapper mapper = new ObjectMapper (createJsonFactory ());
58
+ ObjectMapper mapper = JsonMapper .builder (createJsonFactory ())
59
+ .enable (MapperFeature .ALLOW_FINAL_FIELDS_AS_MUTATORS ) // this is default as of 2.2.0
60
+ .enable (MapperFeature .AUTO_DETECT_FIELDS ) // this is default as of 2.0.0
61
+ .enable (MapperFeature .AUTO_DETECT_GETTERS ) // this is default as of 2.0.0
62
+ .enable (MapperFeature .AUTO_DETECT_IS_GETTERS ) // this is default as of 2.0.0
63
+ .enable (MapperFeature .AUTO_DETECT_SETTERS ) // this is default as of 2.0.0
64
+ .enable (MapperFeature .CAN_OVERRIDE_ACCESS_MODIFIERS ) // this is default as of 2.0.0
65
+ .enable (MapperFeature .USE_STD_BEAN_NAMING )
66
+ .enable (MapperFeature .USE_ANNOTATIONS ) // this is default as of 2.0.0
67
+ .disable (MapperFeature .ACCEPT_CASE_INSENSITIVE_PROPERTIES ) // this is default as of 2.5.0
68
+ .disable (MapperFeature .AUTO_DETECT_CREATORS )
69
+ .disable (MapperFeature .INFER_PROPERTY_MUTATORS )
70
+ .disable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY ) // this is default as of 2.0.0
71
+ .disable (MapperFeature .USE_GETTERS_AS_SETTERS )
72
+ .disable (MapperFeature .USE_WRAPPER_NAME_AS_PROPERTY_NAME ) // this is default as of 2.1.0
73
+ .disable (MapperFeature .USE_STATIC_TYPING ) // this is default as of 2.0.0
74
+ .disable (MapperFeature .REQUIRE_SETTERS_FOR_GETTERS ) // this is default as of 2.0.0
75
+ .build ();
76
+
54
77
SerializationConfig scfg = mapper .getSerializationConfig ();
55
78
scfg = scfg .withFeatures (
56
- SerializationFeature .FAIL_ON_SELF_REFERENCES ,
57
- SerializationFeature .FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS ,
58
- SerializationFeature .WRAP_EXCEPTIONS
59
- );
79
+ SerializationFeature .FAIL_ON_SELF_REFERENCES , // this is default as of 2.4.0
80
+ SerializationFeature .FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS , // this is default as of 2.4.0
81
+ SerializationFeature .WRAP_EXCEPTIONS // this is default as of 2.0.0
82
+ );
60
83
scfg = scfg .withoutFeatures (
61
- SerializationFeature .CLOSE_CLOSEABLE ,
84
+ SerializationFeature .CLOSE_CLOSEABLE , // this is default as of 2.0.0
62
85
SerializationFeature .EAGER_SERIALIZER_FETCH ,
63
86
SerializationFeature .FAIL_ON_EMPTY_BEANS ,
64
87
SerializationFeature .FLUSH_AFTER_WRITE_VALUE ,
65
- SerializationFeature .INDENT_OUTPUT ,
66
- SerializationFeature .ORDER_MAP_ENTRIES_BY_KEYS ,
67
- SerializationFeature .USE_EQUALITY_FOR_OBJECT_ID ,
68
- SerializationFeature .WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS ,
69
- SerializationFeature .WRAP_ROOT_VALUE
70
- );
88
+ SerializationFeature .INDENT_OUTPUT , // this is default as of 2.5.0
89
+ SerializationFeature .ORDER_MAP_ENTRIES_BY_KEYS , // this is default as of 2.0.0
90
+ SerializationFeature .USE_EQUALITY_FOR_OBJECT_ID , // this is default as of 2.3.0
91
+ SerializationFeature .WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS , // this is default as of 2.0.0
92
+ SerializationFeature .WRAP_ROOT_VALUE // this is default as of 2.2.0
93
+ );
71
94
mapper .setConfig (scfg );
72
95
73
96
DeserializationConfig dcfg = mapper .getDeserializationConfig ();
74
97
dcfg = dcfg .withFeatures (
75
98
DeserializationFeature .ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT ,
76
99
DeserializationFeature .ACCEPT_EMPTY_STRING_AS_NULL_OBJECT ,
77
100
DeserializationFeature .ACCEPT_SINGLE_VALUE_AS_ARRAY ,
78
- DeserializationFeature .FAIL_ON_INVALID_SUBTYPE ,
79
- DeserializationFeature .FAIL_ON_UNRESOLVED_OBJECT_IDS ,
101
+ DeserializationFeature .FAIL_ON_INVALID_SUBTYPE , // this is default as of 2.2.0
102
+ DeserializationFeature .FAIL_ON_UNRESOLVED_OBJECT_IDS , // this is default as of 2.5.0
80
103
DeserializationFeature .READ_UNKNOWN_ENUM_VALUES_AS_NULL ,
81
104
DeserializationFeature .UNWRAP_SINGLE_VALUE_ARRAYS ,
82
- DeserializationFeature .WRAP_EXCEPTIONS
83
- );
105
+ DeserializationFeature .WRAP_EXCEPTIONS // this is default as of 2.0.0
106
+ );
84
107
dcfg = dcfg .withoutFeatures (
85
- DeserializationFeature .FAIL_ON_IGNORED_PROPERTIES ,
86
- DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES ,
87
- DeserializationFeature .FAIL_ON_NUMBERS_FOR_ENUMS ,
88
- DeserializationFeature .FAIL_ON_READING_DUP_TREE_KEY ,
108
+ DeserializationFeature .FAIL_ON_IGNORED_PROPERTIES , // this is default as of 2.3.0
109
+ DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES , // this is default as of 2.0.0
110
+ DeserializationFeature .FAIL_ON_NUMBERS_FOR_ENUMS , // this is default as of 2.0.0
111
+ DeserializationFeature .FAIL_ON_READING_DUP_TREE_KEY , // this is default as of 2.3.0
89
112
DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES
90
- );
113
+ );
91
114
mapper .setConfig (dcfg );
92
- mapper .setSerializationInclusion (Include .NON_NULL ); //NON_EMPTY?
93
-
94
- mapper .enable (MapperFeature .ALLOW_FINAL_FIELDS_AS_MUTATORS );
95
- mapper .enable (MapperFeature .AUTO_DETECT_FIELDS );
96
- mapper .enable (MapperFeature .AUTO_DETECT_GETTERS );
97
- mapper .enable (MapperFeature .AUTO_DETECT_IS_GETTERS );
98
- mapper .enable (MapperFeature .AUTO_DETECT_SETTERS );
99
- mapper .enable (MapperFeature .CAN_OVERRIDE_ACCESS_MODIFIERS );
100
- mapper .enable (MapperFeature .USE_STD_BEAN_NAMING );
101
- mapper .enable (MapperFeature .USE_ANNOTATIONS );
102
-
103
- mapper .disable (MapperFeature .ACCEPT_CASE_INSENSITIVE_PROPERTIES );
104
- mapper .disable (MapperFeature .AUTO_DETECT_CREATORS );
105
- mapper .disable (MapperFeature .INFER_PROPERTY_MUTATORS );
106
- mapper .disable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY );
107
- mapper .disable (MapperFeature .USE_GETTERS_AS_SETTERS );
108
- mapper .disable (MapperFeature .USE_WRAPPER_NAME_AS_PROPERTY_NAME );
109
- mapper .disable (MapperFeature .USE_STATIC_TYPING );
110
- mapper .disable (MapperFeature .REQUIRE_SETTERS_FOR_GETTERS );
115
+ mapper .setSerializationInclusion (Include .NON_NULL );
111
116
112
117
SimpleModule module = new SimpleModule ();
113
118
module .addDeserializer (Void .class , new VoidDeserializer ());
114
119
mapper .registerModule (module );
115
120
121
+ mapper .registerModule (new JavaTimeModule ());
122
+ mapper .registerModule (new Jdk8Module ());
123
+
116
124
return mapper ;
117
125
}
118
126
@@ -138,33 +146,42 @@ public Void deserialize(JsonParser parser, DeserializationContext ctx) {
138
146
}
139
147
140
148
private static JsonFactory createJsonFactory () {
141
- JsonFactory factory = new JsonFactory ();
142
- //Json Parser enabled
143
- factory .enable (JsonParser .Feature .ALLOW_NON_NUMERIC_NUMBERS );
144
- factory .enable (JsonParser .Feature .ALLOW_NUMERIC_LEADING_ZEROS );
145
- factory .enable (JsonParser .Feature .ALLOW_SINGLE_QUOTES );
146
- factory .enable (JsonParser .Feature .ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER );
147
- factory .enable (JsonParser .Feature .ALLOW_UNQUOTED_CONTROL_CHARS );
148
- factory .enable (JsonParser .Feature .ALLOW_UNQUOTED_FIELD_NAMES );
149
+ JsonFactory factory = JsonFactory .builder ()
150
+ //Json Read enabled
151
+ .enable (JsonReadFeature .ALLOW_NON_NUMERIC_NUMBERS )
152
+ .enable (JsonReadFeature .ALLOW_LEADING_ZEROS_FOR_NUMBERS )
153
+ .enable (JsonReadFeature .ALLOW_SINGLE_QUOTES )
154
+ .enable (JsonReadFeature .ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER )
155
+ .enable (JsonReadFeature .ALLOW_UNESCAPED_CONTROL_CHARS )
156
+ .enable (JsonReadFeature .ALLOW_UNQUOTED_FIELD_NAMES )
157
+
158
+ //Json Read disabled
159
+ .disable (JsonReadFeature .ALLOW_JAVA_COMMENTS ) // this is default as of 2.10.0
160
+ .disable (JsonReadFeature .ALLOW_YAML_COMMENTS ) // this is default as of 2.10.0
161
+
162
+ //Json Write enabled
163
+ .enable (JsonWriteFeature .QUOTE_FIELD_NAMES ) // this is default as of 2.10.0
164
+ .enable (JsonWriteFeature .WRITE_NAN_AS_STRINGS ) // this is default as of 2.10.0
165
+
166
+ //Json Write disabled
167
+ .disable (JsonWriteFeature .ESCAPE_NON_ASCII ) // this is default as of 2.10.0
168
+ .disable (JsonWriteFeature .WRITE_NUMBERS_AS_STRINGS ) // this is default as of 2.10.0
169
+ .build ();
149
170
150
171
//Json Parser disabled
151
- factory .disable (JsonParser .Feature .ALLOW_COMMENTS );
152
- factory .disable (JsonParser .Feature .ALLOW_YAML_COMMENTS );
153
172
factory .disable (JsonParser .Feature .AUTO_CLOSE_SOURCE );
154
173
factory .disable (JsonParser .Feature .STRICT_DUPLICATE_DETECTION );
155
174
156
- //Json generator enabled
175
+ //Json Generator enabled
157
176
factory .enable (JsonGenerator .Feature .IGNORE_UNKNOWN );
158
- factory .enable (JsonGenerator .Feature .QUOTE_FIELD_NAMES );
159
- factory .enable (JsonGenerator .Feature .QUOTE_NON_NUMERIC_NUMBERS );
160
- //Json generator disabled
177
+
178
+ //Json Generator disabled
161
179
factory .disable (JsonGenerator .Feature .AUTO_CLOSE_JSON_CONTENT );
162
180
factory .disable (JsonGenerator .Feature .AUTO_CLOSE_TARGET );
163
- factory .disable (JsonGenerator .Feature .ESCAPE_NON_ASCII );
164
181
factory .disable (JsonGenerator .Feature .FLUSH_PASSED_TO_STREAM );
165
- factory .disable (JsonGenerator .Feature .STRICT_DUPLICATE_DETECTION );
166
- factory .disable (JsonGenerator .Feature .WRITE_BIGDECIMAL_AS_PLAIN );
167
- factory . disable ( JsonGenerator . Feature . WRITE_NUMBERS_AS_STRINGS );
182
+ factory .disable (JsonGenerator .Feature .STRICT_DUPLICATE_DETECTION ); // this is default as of 2.3.0
183
+ factory .disable (JsonGenerator .Feature .WRITE_BIGDECIMAL_AS_PLAIN ); // this is default as of 2.3.0
184
+
168
185
return factory ;
169
186
}
170
187
@@ -206,7 +223,7 @@ public void toJson(T value, OutputStream output) {
206
223
207
224
private static final class TypeSerializer extends InternalSerializer <Object > {
208
225
public TypeSerializer (ObjectMapper mapper , JavaType type ) {
209
- super (mapper .reader (type ), mapper .writerFor (type ));
226
+ super (mapper .readerFor (type ), mapper .writerFor (type ));
210
227
}
211
228
212
229
public TypeSerializer (ObjectMapper mapper , Type type ) {
@@ -216,7 +233,7 @@ public TypeSerializer(ObjectMapper mapper, Type type) {
216
233
217
234
private static final class ClassSerializer <T > extends InternalSerializer <T > {
218
235
public ClassSerializer (ObjectMapper mapper , Class <T > clazz ) {
219
- super (mapper .reader (clazz ), mapper .writerFor (clazz ));
236
+ super (mapper .readerFor (clazz ), mapper .writerFor (clazz ));
220
237
}
221
238
}
222
239
0 commit comments