Skip to content

Commit 8e3d7f9

Browse files
mp911deodrotbohm
authored andcommitted
DATAMONGO-1517 - Add support for Decimal128 BSON type.
Support Decimal128 as Mongo simple type if present. Decimal128 is stored as NumberDecimal. class Person { String id; Decimal128 decimal128; Person(String id, Decimal128 decimal128) { this.id = id; this.decimal128 = decimal128; } } mongoTemplate.save(new Person("foo", new Decimal128(new BigDecimal("123.456")))); is represented as: { "_id" : "foo", "decimal128" : NumberDecimal("123.456") }
1 parent d2d162d commit 8e3d7f9

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2016 the original author or authors.
2+
* Copyright 2011-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,6 +56,7 @@
5656
* @author Oliver Gierke
5757
* @author Thomas Darimont
5858
* @author Christoph Strobl
59+
* @author Mark Paluch
5960
*/
6061
public class CustomConversions {
6162

@@ -92,7 +93,7 @@ public CustomConversions(List<?> converters) {
9293

9394
this.readingPairs = new LinkedHashSet<ConvertiblePair>();
9495
this.writingPairs = new LinkedHashSet<ConvertiblePair>();
95-
this.customSimpleTypes = new HashSet<Class<?>>();
96+
this.customSimpleTypes = new HashSet<Class<?>>(ReflectiveSimpleTypes.getSupportedSimpleTypes());
9697
this.customReadTargetTypes = new ConcurrentHashMap<ConvertiblePair, CacheValue<Class<?>>>();
9798
this.customWriteTargetTypes = new ConcurrentHashMap<ConvertiblePair, CacheValue<Class<?>>>();
9899
this.rawWriteTargetTypes = new ConcurrentHashMap<Class<?>, CacheValue<Class<?>>>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.convert;
17+
18+
import java.util.Collection;
19+
import java.util.Collections;
20+
21+
import org.springframework.util.ClassUtils;
22+
23+
/**
24+
* {@link ReflectiveSimpleTypes} provides reflective access to MongoDB types that are not consistently available for
25+
* various driver versions.
26+
*
27+
* @author Mark Paluch
28+
* @since 1.10
29+
*/
30+
class ReflectiveSimpleTypes {
31+
32+
private static final boolean HAS_DECIMAL_128 = ClassUtils.isPresent("org.bson.types.Decimal128",
33+
ReflectiveSimpleTypes.class.getClassLoader());
34+
35+
/**
36+
* Returns a {@link Collection} of simple MongoDB types (i.e. natively supported by the MongoDB driver) that are not
37+
* consistently available for various driver versions.
38+
*
39+
* @return a {@link Collection} of simple MongoDB types.
40+
*/
41+
public static Collection<Class<?>> getSupportedSimpleTypes() {
42+
43+
if (HAS_DECIMAL_128) {
44+
return Collections.<Class<?>> singleton(getDecimal128Class());
45+
}
46+
47+
return Collections.emptySet();
48+
}
49+
50+
private static Class<?> getDecimal128Class() {
51+
return ClassUtils.resolveClassName("org.bson.types.Decimal128", ReflectiveSimpleTypes.class.getClassLoader());
52+
}
53+
}

src/main/asciidoc/reference/mapping.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ In addition to these types, Spring Data MongoDB provides a set of built-in conve
126126
| native
127127
| `{"value" : { … }}`
128128

129+
| `Decimal128`
130+
| native
131+
| `{"value" : NumberDecimal(…)}`
132+
129133
| `AtomicInteger` +
130134
calling `get()` before the actual conversion
131135
| converter +

0 commit comments

Comments
 (0)