@ -36,6 +36,8 @@ import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter ;
import org.springframework.core.convert.converter.Converter ;
import org.springframework.core.convert.converter.ConverterFactory ;
import org.springframework.core.convert.converter.ConverterFactory ;
import org.springframework.core.convert.converter.GenericConverter ;
import org.springframework.core.convert.converter.GenericConverter ;
import org.springframework.core.env.Environment ;
import org.springframework.core.env.StandardEnvironment ;
import org.springframework.data.convert.ConverterBuilder ;
import org.springframework.data.convert.ConverterBuilder ;
import org.springframework.data.convert.PropertyValueConversions ;
import org.springframework.data.convert.PropertyValueConversions ;
import org.springframework.data.convert.PropertyValueConverter ;
import org.springframework.data.convert.PropertyValueConverter ;
@ -45,6 +47,11 @@ import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.SimplePropertyValueConversions ;
import org.springframework.data.convert.SimplePropertyValueConversions ;
import org.springframework.data.convert.WritingConverter ;
import org.springframework.data.convert.WritingConverter ;
import org.springframework.data.mapping.model.SimpleTypeHolder ;
import org.springframework.data.mapping.model.SimpleTypeHolder ;
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter ;
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToStringConverter ;
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter ;
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter ;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext ;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty ;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty ;
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes ;
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes ;
import org.springframework.lang.Nullable ;
import org.springframework.lang.Nullable ;
@ -154,11 +161,18 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
private static final Set < Class < ? > > JAVA_DRIVER_TIME_SIMPLE_TYPES = Set . of ( LocalDate . class , LocalTime . class , LocalDateTime . class ) ;
private static final Set < Class < ? > > JAVA_DRIVER_TIME_SIMPLE_TYPES = Set . of ( LocalDate . class , LocalTime . class , LocalDateTime . class ) ;
private boolean useNativeDriverJavaTimeCodecs = false ;
private boolean useNativeDriverJavaTimeCodecs = false ;
private String numericFormat ;
private final List < Object > customConverters = new ArrayList < > ( ) ;
private final List < Object > customConverters = new ArrayList < > ( ) ;
private final PropertyValueConversions internalValueConversion = PropertyValueConversions . simple ( it - > { } ) ;
private final PropertyValueConversions internalValueConversion = PropertyValueConversions . simple ( it - > { } ) ;
private PropertyValueConversions propertyValueConversions = internalValueConversion ;
private PropertyValueConversions propertyValueConversions = internalValueConversion ;
{
Environment env = new StandardEnvironment ( ) ;
boolean flagPresent = env . containsProperty ( "mongo.numeric.format" ) ;
numericFormat = flagPresent ? env . getProperty ( "mongo.numeric.format" , String . class , "string" ) : "string" ;
}
/ * *
/ * *
* Create a { @link MongoConverterConfigurationAdapter } using the provided { @code converters } and our own codecs for
* Create a { @link MongoConverterConfigurationAdapter } using the provided { @code converters } and our own codecs for
* JSR - 310 types .
* JSR - 310 types .
@ -298,6 +312,11 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
return useNativeDriverJavaTimeCodecs ( false ) ;
return useNativeDriverJavaTimeCodecs ( false ) ;
}
}
// TODO: might just be a flag like the time codec?
public MongoConverterConfigurationAdapter numericFormat ( String format ) {
this . numericFormat = format ;
return this ;
}
/ * *
/ * *
* Optionally set the { @link PropertyValueConversions } to be applied during mapping .
* Optionally set the { @link PropertyValueConversions } to be applied during mapping .
* < p >
* < p >
@ -347,15 +366,24 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
svc . init ( ) ;
svc . init ( ) ;
}
}
List < Object > converters = new ArrayList < > ( STORE_CONVERTERS . size ( ) + 7 ) ;
if ( numericFormat . equals ( "string" ) ) {
converters . add ( BigDecimalToStringConverter . INSTANCE ) ;
converters . add ( StringToBigDecimalConverter . INSTANCE ) ;
converters . add ( BigIntegerToStringConverter . INSTANCE ) ;
converters . add ( StringToBigIntegerConverter . INSTANCE ) ;
}
if ( ! useNativeDriverJavaTimeCodecs ) {
if ( ! useNativeDriverJavaTimeCodecs ) {
return new ConverterConfiguration ( STORE_CONVERSIONS , this . customConverters , convertiblePair - > true ,
converters . addAll ( customConverters ) ;
return new ConverterConfiguration ( STORE_CONVERSIONS , converters , convertiblePair - > true ,
this . propertyValueConversions ) ;
this . propertyValueConversions ) ;
}
}
/ *
/ *
* We need to have those converters using UTC as the default ones would go on with the systemDefault .
* We need to have those converters using UTC as the default ones would go on with the systemDefault .
* /
* /
List < Object > converters = new ArrayList < > ( STORE_CONVERTERS . size ( ) + 3 ) ;
converters . add ( DateToUtcLocalDateConverter . INSTANCE ) ;
converters . add ( DateToUtcLocalDateConverter . INSTANCE ) ;
converters . add ( DateToUtcLocalTimeConverter . INSTANCE ) ;
converters . add ( DateToUtcLocalTimeConverter . INSTANCE ) ;
converters . add ( DateToUtcLocalDateTimeConverter . INSTANCE ) ;
converters . add ( DateToUtcLocalDateTimeConverter . INSTANCE ) ;