34
34
import org .springframework .data .mapping .MappingException ;
35
35
import org .springframework .data .util .DirectFieldAccessFallbackBeanWrapper ;
36
36
import org .springframework .util .Assert ;
37
+ import org .springframework .util .ClassUtils ;
37
38
import org .springframework .util .NumberUtils ;
39
+ import org .springframework .util .ObjectUtils ;
38
40
import org .springframework .util .StringUtils ;
39
41
40
42
import com .fasterxml .jackson .annotation .JsonInclude .Include ;
150
152
*/
151
153
public class Jackson2HashMapper implements HashMapper <Object , String , Object > {
152
154
155
+ private static final boolean SOURCE_VERSION_PRESENT = ClassUtils .isPresent ("javax.lang.model.SourceVersion" , Jackson2HashMapper .class .getClassLoader ());
156
+
153
157
private final HashMapperModule HASH_MAPPER_MODULE = new HashMapperModule ();
154
158
155
159
private final ObjectMapper typingMapper ;
@@ -336,10 +340,42 @@ private void flattenElement(String propertyPrefix, Object source, Map<String, Ob
336
340
if (cur .isArray ()) {
337
341
this .flattenCollection (propertyPrefix , cur .elements (), resultMap );
338
342
} else {
343
+ if (nodes .hasNext () && mightBeJavaType (cur )) {
344
+
345
+ JsonNode next = nodes .next ();
346
+
347
+ if (next .isArray ()) {
348
+ this .flattenCollection (propertyPrefix , next .elements (), resultMap );
349
+ }
350
+
351
+ if (cur .asText ().equals ("java.util.Date" )) {
352
+ resultMap .put (propertyPrefix , next .asText ());
353
+ break ;
354
+ }
355
+ if (next .isNumber ()) {
356
+ resultMap .put (propertyPrefix , next .numberValue ());
357
+ break ;
358
+ }
359
+ if (next .isTextual ()) {
360
+
361
+ resultMap .put (propertyPrefix , next .textValue ());
362
+ break ;
363
+ }
364
+ if (next .isBoolean ()) {
365
+
366
+ resultMap .put (propertyPrefix , next .booleanValue ());
367
+ break ;
368
+ }
369
+ if (next .isBinary ()) {
370
+
371
+ try {
372
+ resultMap .put (propertyPrefix , next .binaryValue ());
373
+ } catch (IOException e ) {
374
+ throw new IllegalStateException (String .format ("Cannot read binary value of '%s'" , propertyPrefix ), e );
375
+ }
376
+ break ;
377
+ }
339
378
340
- if (cur .asText ().equals ("java.util.Date" )) {
341
- resultMap .put (propertyPrefix , nodes .next ().asText ());
342
- break ;
343
379
}
344
380
}
345
381
}
@@ -351,6 +387,27 @@ private void flattenElement(String propertyPrefix, Object source, Map<String, Ob
351
387
}
352
388
}
353
389
390
+ private boolean mightBeJavaType (JsonNode node ) {
391
+
392
+ String textValue = node .asText ();
393
+ if (!SOURCE_VERSION_PRESENT ) {
394
+
395
+ if (ObjectUtils .nullSafeEquals (textValue , "java.util.Date" )) {
396
+ return true ;
397
+ }
398
+ if (ObjectUtils .nullSafeEquals (textValue , "java.math.BigInteger" )) {
399
+ return true ;
400
+ }
401
+ if (ObjectUtils .nullSafeEquals (textValue , "java.math.BigDecimal" )) {
402
+ return true ;
403
+ }
404
+
405
+ return false ;
406
+ }
407
+ return javax .lang .model .SourceVersion .isName (textValue );
408
+
409
+ }
410
+
354
411
private void flattenCollection (String propertyPrefix , Iterator <JsonNode > list , Map <String , Object > resultMap ) {
355
412
356
413
int counter = 0 ;
0 commit comments