|
47 | 47 | import org.springframework.data.convert.SimplePropertyValueConversions; |
48 | 48 | import org.springframework.data.convert.WritingConverter; |
49 | 49 | import org.springframework.data.mapping.model.SimpleTypeHolder; |
| 50 | +import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter; |
| 51 | +import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToStringConverter; |
| 52 | +import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter; |
| 53 | +import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter; |
50 | 54 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext; |
51 | 55 | import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; |
52 | 56 | import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes; |
@@ -157,16 +161,16 @@ public static class MongoConverterConfigurationAdapter { |
157 | 161 | private static final Set<Class<?>> JAVA_DRIVER_TIME_SIMPLE_TYPES = Set.of(LocalDate.class, LocalTime.class, LocalDateTime.class); |
158 | 162 |
|
159 | 163 | private boolean useNativeDriverJavaTimeCodecs = false; |
160 | | - private boolean defaultToDecimal128; |
| 164 | + private String numericFormat; |
161 | 165 | private final List<Object> customConverters = new ArrayList<>(); |
162 | 166 |
|
163 | 167 | private final PropertyValueConversions internalValueConversion = PropertyValueConversions.simple(it -> {}); |
164 | 168 | private PropertyValueConversions propertyValueConversions = internalValueConversion; |
165 | 169 |
|
166 | 170 | { |
167 | 171 | Environment env = new StandardEnvironment(); |
168 | | - boolean flagPresent = env.containsProperty("mongo.decimal128.representation"); |
169 | | - defaultToDecimal128 = !flagPresent || env.getProperty("mongo.decimal128.representation", String.class, "string").equals("string"); |
| 172 | + boolean flagPresent = env.containsProperty("mongo.numeric.format"); |
| 173 | + numericFormat = flagPresent ? env.getProperty("mongo.numeric.format", String.class, "string") : "string"; |
170 | 174 | } |
171 | 175 |
|
172 | 176 | /** |
@@ -308,6 +312,10 @@ public MongoConverterConfigurationAdapter useSpringDataJavaTimeCodecs() { |
308 | 312 | return useNativeDriverJavaTimeCodecs(false); |
309 | 313 | } |
310 | 314 |
|
| 315 | + public MongoConverterConfigurationAdapter numericFormat(String format) { |
| 316 | + this.numericFormat = format; |
| 317 | + return this; |
| 318 | + } |
311 | 319 | /** |
312 | 320 | * Optionally set the {@link PropertyValueConversions} to be applied during mapping. |
313 | 321 | * <p> |
@@ -357,17 +365,24 @@ ConverterConfiguration createConverterConfiguration() { |
357 | 365 | svc.init(); |
358 | 366 | } |
359 | 367 |
|
360 | | - // TODO: all the config must go here now and omg get rid of the static blocks!! |
| 368 | + List<Object> converters = new ArrayList<>(STORE_CONVERTERS.size() + 7); |
| 369 | + if(numericFormat.equals("string")) { |
| 370 | + converters.add(BigDecimalToStringConverter.INSTANCE); |
| 371 | + converters.add(StringToBigDecimalConverter.INSTANCE); |
| 372 | + converters.add(BigIntegerToStringConverter.INSTANCE); |
| 373 | + converters.add(StringToBigIntegerConverter.INSTANCE); |
| 374 | + } |
361 | 375 |
|
362 | 376 | if (!useNativeDriverJavaTimeCodecs) { |
363 | | - return new ConverterConfiguration(STORE_CONVERSIONS, this.customConverters, convertiblePair -> true, |
| 377 | + |
| 378 | + converters.addAll(customConverters); |
| 379 | + return new ConverterConfiguration(STORE_CONVERSIONS, converters, convertiblePair -> true, |
364 | 380 | this.propertyValueConversions); |
365 | 381 | } |
366 | 382 |
|
367 | 383 | /* |
368 | 384 | * We need to have those converters using UTC as the default ones would go on with the systemDefault. |
369 | 385 | */ |
370 | | - List<Object> converters = new ArrayList<>(STORE_CONVERTERS.size() + 3); |
371 | 386 | converters.add(DateToUtcLocalDateConverter.INSTANCE); |
372 | 387 | converters.add(DateToUtcLocalTimeConverter.INSTANCE); |
373 | 388 | converters.add(DateToUtcLocalDateTimeConverter.INSTANCE); |
|
0 commit comments