diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java index 14054d84c..6f9daebb3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java @@ -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; * @author Oliver Gierke * @author Thomas Darimont * @author Christoph Strobl + * @author Mark Paluch */ public class CustomConversions { @@ -92,7 +93,7 @@ public class CustomConversions { this.readingPairs = new LinkedHashSet(); this.writingPairs = new LinkedHashSet(); - this.customSimpleTypes = new HashSet>(); + this.customSimpleTypes = new HashSet>(ReflectiveSimpleTypes.getSupportedSimpleTypes()); this.customReadTargetTypes = new ConcurrentHashMap>>(); this.customWriteTargetTypes = new ConcurrentHashMap>>(); this.rawWriteTargetTypes = new ConcurrentHashMap, CacheValue>>(); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReflectiveSimpleTypes.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReflectiveSimpleTypes.java new file mode 100644 index 000000000..f4991c388 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReflectiveSimpleTypes.java @@ -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> getSupportedSimpleTypes() { + + if (HAS_DECIMAL_128) { + return Collections.> singleton(getDecimal128Class()); + } + + return Collections.emptySet(); + } + + private static Class getDecimal128Class() { + return ClassUtils.resolveClassName("org.bson.types.Decimal128", ReflectiveSimpleTypes.class.getClassLoader()); + } +} diff --git a/src/main/asciidoc/reference/mapping.adoc b/src/main/asciidoc/reference/mapping.adoc index d0184a638..af758f77c 100644 --- a/src/main/asciidoc/reference/mapping.adoc +++ b/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 | native | `{"value" : { … }}` +| `Decimal128` +| native +| `{"value" : NumberDecimal(…)}` + | `AtomicInteger` + calling `get()` before the actual conversion | converter +