1515 */
1616package org .springframework .data .mongodb .core .convert ;
1717
18- import java .time .Instant ;
1918import java .time .LocalDate ;
2019import java .time .LocalDateTime ;
2120import java .time .LocalTime ;
3332import java .util .function .Consumer ;
3433
3534import org .jspecify .annotations .Nullable ;
35+
3636import org .springframework .core .convert .TypeDescriptor ;
3737import org .springframework .core .convert .converter .Converter ;
3838import org .springframework .core .convert .converter .ConverterFactory ;
4242import org .springframework .data .convert .PropertyValueConverter ;
4343import org .springframework .data .convert .PropertyValueConverterFactory ;
4444import org .springframework .data .convert .PropertyValueConverterRegistrar ;
45- import org .springframework .data .convert .ReadingConverter ;
4645import org .springframework .data .convert .SimplePropertyValueConversions ;
4746import org .springframework .data .convert .WritingConverter ;
4847import 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 ;
5348import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
5449import org .springframework .data .mongodb .core .mapping .MongoSimpleTypes ;
5550import org .springframework .lang .Contract ;
6863 */
6964public class MongoCustomConversions extends org .springframework .data .convert .CustomConversions {
7065
71- private static final StoreConversions STORE_CONVERSIONS ;
7266 private static final List <Object > STORE_CONVERTERS ;
7367
7468 static {
@@ -80,7 +74,6 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
8074 converters .addAll (GeoConverters .getConvertersToRegister ());
8175
8276 STORE_CONVERTERS = Collections .unmodifiableList (converters );
83- STORE_CONVERSIONS = StoreConversions .of (MongoSimpleTypes .HOLDER , STORE_CONVERTERS );
8477 }
8578
8679 /**
@@ -156,7 +149,8 @@ public static class MongoConverterConfigurationAdapter {
156149 * List of {@literal java.time} types having different representation when rendered via the native
157150 * {@link org.bson.codecs.Codec} than the Spring Data {@link Converter}.
158151 */
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 );
160154
161155 private boolean useNativeDriverJavaTimeCodecs = false ;
162156 private BigDecimalRepresentation bigDecimals = BigDecimalRepresentation .DECIMAL128 ;
@@ -326,6 +320,7 @@ public MongoConverterConfigurationAdapter bigDecimal(BigDecimalRepresentation re
326320 this .bigDecimals = representation ;
327321 return this ;
328322 }
323+
329324 /**
330325 * Optionally set the {@link PropertyValueConversions} to be applied during mapping.
331326 * <p>
@@ -375,72 +370,40 @@ ConverterConfiguration createConverterConfiguration() {
375370 svc .init ();
376371 }
377372
378- List <Object > converters = new ArrayList <>(STORE_CONVERTERS .size () + 7 );
373+ List <Object > storeConverters = new ArrayList <>(STORE_CONVERTERS .size () + 10 );
379374
380375 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 ());
386377 }
387378
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 ());
393381 }
394382
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 ) {
402384
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 );
405390
406- return new ConverterConfiguration (storeConversions , this .customConverters , convertiblePair -> {
391+ StoreConversions storeConversions = StoreConversions
392+ .of (new SimpleTypeHolder (JAVA_DRIVER_TIME_SIMPLE_TYPES , MongoSimpleTypes .HOLDER ), storeConverters );
407393
408- // Avoid default registrations
394+ return new ConverterConfiguration (storeConversions , this .customConverters , convertiblePair -> {
395+
396+ // Avoid default registrations
409397
410398 return !JAVA_DRIVER_TIME_SIMPLE_TYPES .contains (convertiblePair .getSourceType ())
411399 || !Date .class .isAssignableFrom (convertiblePair .getTargetType ());
412400 }, this .propertyValueConversions );
413- }
414-
415- @ ReadingConverter
416- private enum DateToUtcLocalDateTimeConverter implements Converter <Date , LocalDateTime > {
417-
418- INSTANCE ;
419401
420- @ Override
421- public LocalDateTime convert (Date source ) {
422- return LocalDateTime .ofInstant (Instant .ofEpochMilli (source .getTime ()), ZoneId .of ("UTC" ));
423402 }
424- }
425-
426- @ ReadingConverter
427- private enum DateToUtcLocalTimeConverter implements Converter <Date , LocalTime > {
428- INSTANCE ;
429403
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 );
444407 }
445408
446409 private boolean hasDefaultPropertyValueConversions () {
0 commit comments