15
15
*/
16
16
package org .springframework .data .mongodb .core .convert ;
17
17
18
- import java .time .Instant ;
19
18
import java .time .LocalDate ;
20
19
import java .time .LocalDateTime ;
21
20
import java .time .LocalTime ;
33
32
import java .util .function .Consumer ;
34
33
35
34
import org .jspecify .annotations .Nullable ;
35
+
36
36
import org .springframework .core .convert .TypeDescriptor ;
37
37
import org .springframework .core .convert .converter .Converter ;
38
38
import org .springframework .core .convert .converter .ConverterFactory ;
42
42
import org .springframework .data .convert .PropertyValueConverter ;
43
43
import org .springframework .data .convert .PropertyValueConverterFactory ;
44
44
import org .springframework .data .convert .PropertyValueConverterRegistrar ;
45
- import org .springframework .data .convert .ReadingConverter ;
46
45
import org .springframework .data .convert .SimplePropertyValueConversions ;
47
46
import org .springframework .data .convert .WritingConverter ;
48
47
import org .springframework .data .mapping .model .SimpleTypeHolder ;
49
- import org .springframework .data .mongodb .core .convert .MongoConverters .BigDecimalToStringConverter ;
50
- import org .springframework .data .mongodb .core .convert .MongoConverters .BigIntegerToStringConverter ;
51
- import org .springframework .data .mongodb .core .convert .MongoConverters .StringToBigDecimalConverter ;
52
- import org .springframework .data .mongodb .core .convert .MongoConverters .StringToBigIntegerConverter ;
53
48
import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
54
49
import org .springframework .data .mongodb .core .mapping .MongoSimpleTypes ;
55
50
import org .springframework .lang .Contract ;
68
63
*/
69
64
public class MongoCustomConversions extends org .springframework .data .convert .CustomConversions {
70
65
71
- private static final StoreConversions STORE_CONVERSIONS ;
72
66
private static final List <Object > STORE_CONVERTERS ;
73
67
74
68
static {
@@ -80,7 +74,6 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
80
74
converters .addAll (GeoConverters .getConvertersToRegister ());
81
75
82
76
STORE_CONVERTERS = Collections .unmodifiableList (converters );
83
- STORE_CONVERSIONS = StoreConversions .of (MongoSimpleTypes .HOLDER , STORE_CONVERTERS );
84
77
}
85
78
86
79
/**
@@ -156,7 +149,8 @@ public static class MongoConverterConfigurationAdapter {
156
149
* List of {@literal java.time} types having different representation when rendered via the native
157
150
* {@link org.bson.codecs.Codec} than the Spring Data {@link Converter}.
158
151
*/
159
- private static final Set <Class <?>> JAVA_DRIVER_TIME_SIMPLE_TYPES = Set .of (LocalDate .class , LocalTime .class , LocalDateTime .class );
152
+ private static final Set <Class <?>> JAVA_DRIVER_TIME_SIMPLE_TYPES = Set .of (LocalDate .class , LocalTime .class ,
153
+ LocalDateTime .class );
160
154
161
155
private boolean useNativeDriverJavaTimeCodecs = false ;
162
156
private BigDecimalRepresentation bigDecimals = BigDecimalRepresentation .DECIMAL128 ;
@@ -326,6 +320,7 @@ public MongoConverterConfigurationAdapter bigDecimal(BigDecimalRepresentation re
326
320
this .bigDecimals = representation ;
327
321
return this ;
328
322
}
323
+
329
324
/**
330
325
* Optionally set the {@link PropertyValueConversions} to be applied during mapping.
331
326
* <p>
@@ -375,72 +370,40 @@ ConverterConfiguration createConverterConfiguration() {
375
370
svc .init ();
376
371
}
377
372
378
- List <Object > converters = new ArrayList <>(STORE_CONVERTERS .size () + 7 );
373
+ List <Object > storeConverters = new ArrayList <>(STORE_CONVERTERS .size () + 10 );
379
374
380
375
if (bigDecimals == BigDecimalRepresentation .STRING ) {
381
-
382
- converters .add (BigDecimalToStringConverter .INSTANCE );
383
- converters .add (StringToBigDecimalConverter .INSTANCE );
384
- converters .add (BigIntegerToStringConverter .INSTANCE );
385
- converters .add (StringToBigIntegerConverter .INSTANCE );
376
+ storeConverters .addAll (MongoConverters .getBigNumberStringConverters ());
386
377
}
387
378
388
- if (!useNativeDriverJavaTimeCodecs ) {
389
-
390
- converters .addAll (customConverters );
391
- return new ConverterConfiguration (STORE_CONVERSIONS , converters , convertiblePair -> true ,
392
- this .propertyValueConversions );
379
+ if (bigDecimals == BigDecimalRepresentation .DECIMAL128 ) {
380
+ storeConverters .addAll (MongoConverters .getBigNumberDecimal128Converters ());
393
381
}
394
382
395
- /*
396
- * We need to have those converters using UTC as the default ones would go on with the systemDefault.
397
- */
398
- converters .add (DateToUtcLocalDateConverter .INSTANCE );
399
- converters .add (DateToUtcLocalTimeConverter .INSTANCE );
400
- converters .add (DateToUtcLocalDateTimeConverter .INSTANCE );
401
- converters .addAll (STORE_CONVERTERS );
383
+ if (useNativeDriverJavaTimeCodecs ) {
402
384
403
- StoreConversions storeConversions = StoreConversions
404
- .of (new SimpleTypeHolder (JAVA_DRIVER_TIME_SIMPLE_TYPES , MongoSimpleTypes .HOLDER ), converters );
385
+ /*
386
+ * We need to have those converters using UTC as the default ones would go on with the systemDefault.
387
+ */
388
+ storeConverters .addAll (MongoConverters .getDateToUtcConverters ());
389
+ storeConverters .addAll (STORE_CONVERTERS );
405
390
406
- return new ConverterConfiguration (storeConversions , this .customConverters , convertiblePair -> {
391
+ StoreConversions storeConversions = StoreConversions
392
+ .of (new SimpleTypeHolder (JAVA_DRIVER_TIME_SIMPLE_TYPES , MongoSimpleTypes .HOLDER ), storeConverters );
407
393
408
- // Avoid default registrations
394
+ return new ConverterConfiguration (storeConversions , this .customConverters , convertiblePair -> {
395
+
396
+ // Avoid default registrations
409
397
410
398
return !JAVA_DRIVER_TIME_SIMPLE_TYPES .contains (convertiblePair .getSourceType ())
411
399
|| !Date .class .isAssignableFrom (convertiblePair .getTargetType ());
412
400
}, this .propertyValueConversions );
413
- }
414
-
415
- @ ReadingConverter
416
- private enum DateToUtcLocalDateTimeConverter implements Converter <Date , LocalDateTime > {
417
-
418
- INSTANCE ;
419
401
420
- @ Override
421
- public LocalDateTime convert (Date source ) {
422
- return LocalDateTime .ofInstant (Instant .ofEpochMilli (source .getTime ()), ZoneId .of ("UTC" ));
423
402
}
424
- }
425
-
426
- @ ReadingConverter
427
- private enum DateToUtcLocalTimeConverter implements Converter <Date , LocalTime > {
428
- INSTANCE ;
429
403
430
- @ Override
431
- public LocalTime convert (Date source ) {
432
- return DateToUtcLocalDateTimeConverter .INSTANCE .convert (source ).toLocalTime ();
433
- }
434
- }
435
-
436
- @ ReadingConverter
437
- private enum DateToUtcLocalDateConverter implements Converter <Date , LocalDate > {
438
- INSTANCE ;
439
-
440
- @ Override
441
- public LocalDate convert (Date source ) {
442
- return DateToUtcLocalDateTimeConverter .INSTANCE .convert (source ).toLocalDate ();
443
- }
404
+ storeConverters .addAll (STORE_CONVERTERS );
405
+ return new ConverterConfiguration (StoreConversions .of (MongoSimpleTypes .createSimpleTypeHolder (), storeConverters ),
406
+ this .customConverters , convertiblePair -> true , this .propertyValueConversions );
444
407
}
445
408
446
409
private boolean hasDefaultPropertyValueConversions () {
0 commit comments