19
19
import tools .jackson .core .JsonGenerator ;
20
20
import tools .jackson .core .JsonParser ;
21
21
import tools .jackson .core .Version ;
22
- import tools .jackson .databind .DatabindContext ;
23
22
import tools .jackson .databind .DefaultTyping ;
24
23
import tools .jackson .databind .DeserializationContext ;
25
24
import tools .jackson .databind .DeserializationFeature ;
29
28
import tools .jackson .databind .SerializationContext ;
30
29
import tools .jackson .databind .ValueDeserializer ;
31
30
import tools .jackson .databind .ValueSerializer ;
31
+ import tools .jackson .databind .cfg .MapperBuilder ;
32
32
import tools .jackson .databind .deser .jdk .JavaUtilCalendarDeserializer ;
33
33
import tools .jackson .databind .json .JsonMapper ;
34
+ import tools .jackson .databind .json .JsonMapper .Builder ;
34
35
import tools .jackson .databind .jsontype .BasicPolymorphicTypeValidator ;
35
- import tools .jackson .databind .jsontype .BasicPolymorphicTypeValidator .TypeMatcher ;
36
36
import tools .jackson .databind .module .SimpleDeserializers ;
37
37
import tools .jackson .databind .module .SimpleSerializers ;
38
38
import tools .jackson .databind .ser .Serializers ;
48
48
import java .util .HashMap ;
49
49
import java .util .Iterator ;
50
50
import java .util .LinkedHashMap ;
51
- import java .util .LinkedHashSet ;
52
51
import java .util .List ;
53
52
import java .util .Map ;
54
53
import java .util .Map .Entry ;
55
54
import java .util .Set ;
56
55
import java .util .TimeZone ;
56
+ import java .util .function .Consumer ;
57
+ import java .util .function .Supplier ;
57
58
58
59
import org .springframework .data .mapping .MappingException ;
59
60
import org .springframework .data .redis .support .collections .CollectionUtils ;
@@ -159,87 +160,25 @@ public class Jackson3HashMapper implements HashMapper<Object, String, Object> {
159
160
Jackson3HashMapper .class .getClassLoader ());
160
161
161
162
private final ObjectMapper typingMapper ;
162
- private final ObjectMapper untypedMapper ;
163
163
private final boolean flatten ;
164
164
165
- /**
166
- * Creates new {@link Jackson3HashMapper} with a default {@link ObjectMapper}.
167
- *
168
- * @param flatten boolean used to configure whether JSON de/serialized {@link Object} properties will be un/flattened
169
- * using {@literal dot notation}, or whether to retain the hierarchical node structure created by Jackson.
170
- */
171
- public Jackson3HashMapper (boolean flatten ) {
165
+ public Jackson3HashMapper (
166
+ Consumer <MapperBuilder <? extends ObjectMapper , ? extends MapperBuilder <?, ?>>> jsonMapperBuilder ,
167
+ boolean flatten ) {
168
+ this (((Supplier <JsonMapper >) () -> {
169
+ Builder builder = JsonMapper .builder ();
170
+ jsonMapperBuilder .accept (builder );
171
+ return builder .build ();
172
+ }).get (), flatten );
173
+ }
172
174
173
- this (JsonMapper .builder ().findAndAddModules ().addModules (new HashMapperModule ())
174
- .activateDefaultTypingAsProperty (BasicPolymorphicTypeValidator .builder ().allowIfBaseType (Object .class ).allowIfSubType (new TypeMatcher () {
175
- @ Override
176
- public boolean match (DatabindContext ctxt , Class <?> clazz ) {
177
- return true ;
178
- }
179
- }).build (),
180
- DefaultTyping .NON_FINAL_AND_ENUMS , "@class" )
181
- .configure (DeserializationFeature .FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY , false )
175
+ public static void preconfigure (MapperBuilder <? extends ObjectMapper , ? extends MapperBuilder <?, ?>> builder ) {
176
+ builder .findAndAddModules ().addModules (new HashMapperModule ())
177
+ .activateDefaultTypingAsProperty (BasicPolymorphicTypeValidator .builder ().allowIfBaseType (Object .class )
178
+ .allowIfSubType ((ctx , clazz ) -> true ).build (), DefaultTyping .NON_FINAL_AND_ENUMS , "@class" )
179
+ .configure (DeserializationFeature .FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY , false )
182
180
.configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
183
- .changeDefaultPropertyInclusion (value -> value .withValueInclusion (Include .NON_NULL )).build (), false );
184
-
185
- //// this(new Supplier<ObjectMapper>() {
186
- // @Override
187
- // public ObjectMapper get() {
188
- //// return JsonMapper.builder()
189
- //// .findAndAddModules()
190
- //// .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
191
- //// .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
192
- //// .build();
193
- // return null;
194
- // }
195
- // }.get(), false);
196
- // this(new ObjectMapper() {
197
- //
198
- // @Override
199
- // protected DeserializationContextExt _deserializationContext(JsonParser p) {
200
- // return super._deserializationContext(p);
201
- // }
202
- //
203
- // @Override
204
- // protected TypeResolverBuilder<?> _constructDefaultTypeResolverBuilder(DefaultTyping applicability,
205
- // PolymorphicTypeValidator typeValidator) {
206
- //
207
- // return new DefaultTypeResolverBuilder(applicability, typeValidator, ) {
208
- //
209
- // public boolean useForType(JavaType type) {
210
- //
211
- // if (type.isPrimitive()) {
212
- // return false;
213
- // }
214
- //
215
- // if (flatten && (type.isTypeOrSubTypeOf(Number.class) || type.isEnumType())) {
216
- // return false;
217
- // }
218
- //
219
- // if (EVERYTHING.equals(_appliesFor)) {
220
- // return !TreeNode.class.isAssignableFrom(type.getRawClass());
221
- // }
222
- //
223
- // return super.useForType(type);
224
- // }
225
- // };
226
- // }
227
- // }.findAndRegisterModules(), flatten);
228
- // },flatten);
229
-
230
- // this.typingMapper.de.activateDefaultTyping(this.typingMapper.getPolymorphicTypeValidator(),
231
- // DefaultTyping.EVERYTHING, As.PROPERTY);
232
- // this.typingMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
233
- //
234
- // if(flatten) {
235
- // this.typingMapper.disable(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES);
236
- // }
237
- //
238
- // // Prevent splitting time types into arrays. E
239
- // this.typingMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
240
- // this.typingMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
241
- // this.typingMapper.setSerializationInclusion(Include.NON_NULL);
242
- // this.typingMapper.registerModule(new HashMapperModule());
181
+ .changeDefaultPropertyInclusion (value -> value .withValueInclusion (Include .NON_NULL ));
243
182
}
244
183
245
184
/**
@@ -256,19 +195,14 @@ public Jackson3HashMapper(ObjectMapper mapper, boolean flatten) {
256
195
257
196
this .flatten = flatten ;
258
197
this .typingMapper = mapper ;
259
- this .untypedMapper = new ObjectMapper ();
260
- // this.untypedMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
261
- // this.untypedMapper.setSerializationInclusion(Include.NON_NULL);
262
- // this.untypedMapper.findAndRegisterModules();
263
198
}
264
199
265
200
@ Override
266
201
@ SuppressWarnings ("unchecked" )
267
202
public Map <String , Object > toHash (Object source ) {
268
203
269
204
JsonNode tree = this .typingMapper .valueToTree (source );
270
-
271
- return this .flatten ? flattenMap (tree .properties ()) : this .untypedMapper .convertValue (tree , Map .class );
205
+ return this .flatten ? flattenMap (tree .properties ()) : JsonMapper .shared ().convertValue (tree , Map .class );
272
206
}
273
207
274
208
@ Override
@@ -279,13 +213,13 @@ public Object fromHash(Map<String, Object> hash) {
279
213
if (this .flatten ) {
280
214
281
215
Map <String , Object > unflattenedHash = doUnflatten (hash );
282
- byte [] unflattenedHashedBytes = this . untypedMapper .writeValueAsBytes (unflattenedHash );
216
+ byte [] unflattenedHashedBytes = JsonMapper . shared () .writeValueAsBytes (unflattenedHash );
283
217
Object hashedObject = this .typingMapper .reader ().forType (Object .class ).readValue (unflattenedHashedBytes );
284
218
285
219
return hashedObject ;
286
220
}
287
221
288
- return this .typingMapper .treeToValue (this . untypedMapper .valueToTree (hash ), Object .class );
222
+ return this .typingMapper .treeToValue (JsonMapper . shared () .valueToTree (hash ), Object .class );
289
223
290
224
} catch (Exception ex ) {
291
225
throw new MappingException (ex .getMessage (), ex );
@@ -295,8 +229,8 @@ public Object fromHash(Map<String, Object> hash) {
295
229
@ SuppressWarnings ("unchecked" )
296
230
private Map <String , Object > doUnflatten (Map <String , Object > source ) {
297
231
298
- Map <String , Object > result = new LinkedHashMap <>( );
299
- Set <String > treatSeparate = new LinkedHashSet <>( );
232
+ Map <String , Object > result = org . springframework . util . CollectionUtils . newLinkedHashMap ( source . size () );
233
+ Set <String > treatSeparate = org . springframework . util . CollectionUtils . newLinkedHashSet ( source . size () );
300
234
301
235
for (Entry <String , Object > entry : source .entrySet ()) {
302
236
@@ -573,112 +507,4 @@ public Calendar deserialize(JsonParser p, DeserializationContext ctxt) throws Ja
573
507
return utc ;
574
508
}
575
509
}
576
-
577
- // /**
578
- // * {@link JsonDeserializer} for {@link Date} objects without considering type hints.
579
- // */
580
- // private static class UntypedDateDeserializer extends JsonDeserializer<Date> {
581
- //
582
- // private final JsonDeserializer<?> delegate = new UntypedObjectDeserializer(null, null);
583
- //
584
- // @Override
585
- // public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer)
586
- // throws IOException {
587
- // return deserialize(p, ctxt);
588
- // }
589
- //
590
- // @Override
591
- // public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
592
- //
593
- // Object value = delegate.deserialize(p, ctxt);
594
- //
595
- // if (value instanceof Date) {
596
- // return (Date) value;
597
- // }
598
- //
599
- // try {
600
- // return ctxt.getConfig().getDateFormat().parse(value.toString());
601
- // } catch (ParseException ignore) {
602
- // return new Date(NumberUtils.parseNumber(value.toString(), Long.class));
603
- // }
604
- // }
605
- // }
606
- //
607
- // /**
608
- // * {@link JsonDeserializer} for {@link Calendar} objects without considering type hints.
609
- // */
610
- // private static class UntypedCalendarDeserializer extends JsonDeserializer<Calendar> {
611
- //
612
- // private final UntypedDateDeserializer dateDeserializer = new UntypedDateDeserializer();
613
- //
614
- // @Override
615
- // public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer)
616
- // throws IOException {
617
- // return deserialize(p, ctxt);
618
- // }
619
- //
620
- // @Override
621
- // public Calendar deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
622
- //
623
- // Date date = dateDeserializer.deserialize(p, ctxt);
624
- //
625
- // if (date != null) {
626
- // Calendar calendar = Calendar.getInstance();
627
- // calendar.setTime(date);
628
- // return calendar;
629
- // }
630
- //
631
- // return null;
632
- // }
633
- // }
634
- //
635
- // /**
636
- // * Untyped {@link JsonSerializer} to serialize plain values without writing JSON type hints.
637
- // *
638
- // * @param <T>
639
- // */
640
- // private static class UntypedSerializer<T> extends JsonSerializer<T> {
641
- //
642
- // private final JsonSerializer<T> delegate;
643
- //
644
- // UntypedSerializer(JsonSerializer<T> delegate) {
645
- // this.delegate = delegate;
646
- // }
647
- //
648
- // @Override
649
- // public void serializeWithType(T value, JsonGenerator jsonGenerator, SerializerProvider serializers,
650
- // TypeSerializer typeSerializer) throws IOException {
651
- //
652
- // serialize(value, jsonGenerator, serializers);
653
- // }
654
- //
655
- // @Override
656
- // public void serialize(@Nullable T value, JsonGenerator jsonGenerator, SerializerProvider serializers)
657
- // throws IOException {
658
- //
659
- // if (value != null) {
660
- // delegate.serialize(value, jsonGenerator, serializers);
661
- // } else {
662
- // serializers.defaultSerializeNull(jsonGenerator);
663
- // }
664
- // }
665
- // }
666
- //
667
- // private static class DateToTimestampSerializer extends DateSerializer {
668
- //
669
- // // Prevent splitting to array.
670
- // @Override
671
- // protected boolean _asTimestamp(SerializerProvider serializers) {
672
- // return true;
673
- // }
674
- // }
675
- //
676
- // private static class CalendarToTimestampSerializer extends CalendarSerializer {
677
- //
678
- // // Prevent splitting to array.
679
- // @Override
680
- // protected boolean _asTimestamp(SerializerProvider serializers) {
681
- // return true;
682
- // }
683
- // }
684
510
}
0 commit comments