Browse Source

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") }
pull/410/merge
Mark Paluch 9 years ago committed by Oliver Gierke
parent
commit
a644187131
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java
  2. 53
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReflectiveSimpleTypes.java
  3. 4
      src/main/asciidoc/reference/mapping.adoc

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -56,6 +56,7 @@ import org.springframework.util.Assert; @@ -56,6 +56,7 @@ import org.springframework.util.Assert;
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
*/
public class CustomConversions {
@ -92,7 +93,7 @@ public class CustomConversions { @@ -92,7 +93,7 @@ public class CustomConversions {
this.readingPairs = new LinkedHashSet<ConvertiblePair>();
this.writingPairs = new LinkedHashSet<ConvertiblePair>();
this.customSimpleTypes = new HashSet<Class<?>>();
this.customSimpleTypes = new HashSet<Class<?>>(ReflectiveSimpleTypes.getSupportedSimpleTypes());
this.customReadTargetTypes = new ConcurrentHashMap<ConvertiblePair, CacheValue<Class<?>>>();
this.customWriteTargetTypes = new ConcurrentHashMap<ConvertiblePair, CacheValue<Class<?>>>();
this.rawWriteTargetTypes = new ConcurrentHashMap<Class<?>, CacheValue<Class<?>>>();

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

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.convert;
import java.util.Collection;
import java.util.Collections;
import org.springframework.util.ClassUtils;
/**
* {@link ReflectiveSimpleTypes} provides reflective access to MongoDB types that are not consistently available for
* various driver versions.
*
* @author Mark Paluch
* @since 1.10
*/
class ReflectiveSimpleTypes {
private static final boolean HAS_DECIMAL_128 = ClassUtils.isPresent("org.bson.types.Decimal128",
ReflectiveSimpleTypes.class.getClassLoader());
/**
* Returns a {@link Collection} of simple MongoDB types (i.e. natively supported by the MongoDB driver) that are not
* consistently available for various driver versions.
*
* @return a {@link Collection} of simple MongoDB types.
*/
public static Collection<Class<?>> getSupportedSimpleTypes() {
if (HAS_DECIMAL_128) {
return Collections.<Class<?>> singleton(getDecimal128Class());
}
return Collections.emptySet();
}
private static Class<?> getDecimal128Class() {
return ClassUtils.resolveClassName("org.bson.types.Decimal128", ReflectiveSimpleTypes.class.getClassLoader());
}
}

4
src/main/asciidoc/reference/mapping.adoc

@ -126,6 +126,10 @@ In addition to these types, Spring Data MongoDB provides a set of built-in conve @@ -126,6 +126,10 @@ In addition to these types, Spring Data MongoDB provides a set of built-in conve
| native
| `{"value" : { … }}`
| `Decimal128`
| native
| `{"value" : NumberDecimal(…)}`
| `AtomicInteger` +
calling `get()` before the actual conversion
| converter +

Loading…
Cancel
Save