53
53
import org .springframework .core .convert .converter .ConditionalConverter ;
54
54
import org .springframework .core .convert .converter .Converter ;
55
55
import org .springframework .core .convert .converter .ConverterFactory ;
56
+ import org .springframework .core .env .Environment ;
57
+ import org .springframework .core .env .StandardEnvironment ;
56
58
import org .springframework .data .convert .ReadingConverter ;
57
59
import org .springframework .data .convert .WritingConverter ;
58
60
import org .springframework .data .domain .Vector ;
@@ -89,14 +91,27 @@ private MongoConverters() {}
89
91
*/
90
92
static Collection <Object > getConvertersToRegister () {
91
93
94
+ Environment env = new StandardEnvironment ();
95
+ boolean flagPresent = env .containsProperty ("mongo.decimal128.representation" );
96
+ boolean decimalToString = !flagPresent || env .getProperty ("mongo.decimal128.representation" , String .class , "string" ).equals ("string" );
97
+
92
98
List <Object > converters = new ArrayList <>();
93
99
94
- converters .add (BigDecimalToStringConverter .INSTANCE );
100
+ if (decimalToString ) {
101
+ converters .add (BigDecimalToStringConverter .INSTANCE );
102
+ converters .add (StringToBigDecimalConverter .INSTANCE );
103
+ } else {
104
+ //converters.add(new NumberToNumberConverter<>())
105
+ }
106
+
95
107
converters .add (BigDecimalToDecimal128Converter .INSTANCE );
96
- converters .add (StringToBigDecimalConverter .INSTANCE );
97
108
converters .add (Decimal128ToBigDecimalConverter .INSTANCE );
98
- converters .add (BigIntegerToStringConverter .INSTANCE );
99
- converters .add (StringToBigIntegerConverter .INSTANCE );
109
+
110
+ if (decimalToString ) {
111
+ converters .add (BigIntegerToStringConverter .INSTANCE );
112
+ converters .add (StringToBigIntegerConverter .INSTANCE );
113
+ }
114
+
100
115
converters .add (URLToStringConverter .INSTANCE );
101
116
converters .add (StringToURLConverter .INSTANCE );
102
117
converters .add (DocumentToStringConverter .INSTANCE );
@@ -111,6 +126,7 @@ static Collection<Object> getConvertersToRegister() {
111
126
converters .add (IntegerToAtomicIntegerConverter .INSTANCE );
112
127
converters .add (BinaryToByteArrayConverter .INSTANCE );
113
128
converters .add (BsonTimestampToInstantConverter .INSTANCE );
129
+ converters .add (NumberToNumberConverterFactory .INSTANCE );
114
130
115
131
converters .add (VectorToBsonArrayConverter .INSTANCE );
116
132
converters .add (ListToVectorConverter .INSTANCE );
@@ -174,12 +190,20 @@ public ObjectId convert(BigInteger source) {
174
190
}
175
191
}
176
192
177
- enum BigDecimalToStringConverter implements Converter <BigDecimal , String > {
193
+ enum BigDecimalToStringConverter implements Converter <BigDecimal , String >, ConditionalConverter {
178
194
INSTANCE ;
179
195
196
+ Environment env = new StandardEnvironment ();
197
+
180
198
public String convert (BigDecimal source ) {
181
199
return source .toString ();
182
200
}
201
+
202
+ @ Override
203
+ public boolean matches (TypeDescriptor sourceType , TypeDescriptor targetType ) {
204
+ boolean flagPresent = env .containsProperty ("mongo.decimal128.representation" );
205
+ return !flagPresent || env .getProperty ("mongo.decimal128.representation" , String .class , "string" ).equals ("string" );
206
+ }
183
207
}
184
208
185
209
/**
@@ -212,12 +236,21 @@ public BigDecimal convert(Decimal128 source) {
212
236
}
213
237
}
214
238
215
- enum BigIntegerToStringConverter implements Converter <BigInteger , String > {
239
+ enum BigIntegerToStringConverter implements Converter <BigInteger , String >, ConditionalConverter {
216
240
INSTANCE ;
217
241
242
+ Environment env = new StandardEnvironment ();
243
+
218
244
public String convert (BigInteger source ) {
219
245
return source .toString ();
220
246
}
247
+
248
+ @ Override
249
+ public boolean matches (TypeDescriptor sourceType , TypeDescriptor targetType ) {
250
+
251
+ boolean flagPresent = env .containsProperty ("mongo.decimal128.representation" );
252
+ return !flagPresent || env .getProperty ("mongo.decimal128.representation" , String .class , "string" ).equals ("string" );
253
+ }
221
254
}
222
255
223
256
enum StringToBigIntegerConverter implements Converter <String , BigInteger > {
@@ -414,6 +447,17 @@ public NumberToNumberConverter(Class<T> targetType) {
414
447
@ Override
415
448
public T convert (Number source ) {
416
449
450
+ if (targetType == Decimal128 .class ) {
451
+
452
+ if (source instanceof BigDecimal bigDecimal ) {
453
+ return targetType .cast (BigDecimalToDecimal128Converter .INSTANCE .convert (bigDecimal ));
454
+ }
455
+
456
+ if (source instanceof BigInteger bigInteger ) {
457
+ return targetType .cast (new Decimal128 (bigInteger .longValueExact ()));
458
+ }
459
+ }
460
+
417
461
if (source instanceof AtomicInteger atomicInteger ) {
418
462
return NumberUtils .convertNumberToTargetClass (atomicInteger .get (), this .targetType );
419
463
}
0 commit comments