DATADOC-171 - Convert BigDecimals to String by default.

Unfortunately MongoDB can't handle BigDecimal instances by default thus we have to provide a default serialization. We do by simply serializing it into a String and reading it back. Can be customized to register a custom Converter with the MongoConverter.
This commit is contained in:
Oliver Gierke
2011-07-07 21:35:54 +02:00
parent 2e3f2c602c
commit 2d5f41f65c
5 changed files with 88 additions and 19 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.document.mongodb.convert;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
@@ -27,10 +28,12 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.document.mongodb.convert.ObjectIdConverters.BigIntegerToObjectIdConverter;
import org.springframework.data.document.mongodb.convert.ObjectIdConverters.ObjectIdToBigIntegerConverter;
import org.springframework.data.document.mongodb.convert.ObjectIdConverters.ObjectIdToStringConverter;
import org.springframework.data.document.mongodb.convert.ObjectIdConverters.StringToObjectIdConverter;
import org.springframework.data.document.mongodb.convert.MongoConverters.BigDecimalToStringConverter;
import org.springframework.data.document.mongodb.convert.MongoConverters.BigIntegerToObjectIdConverter;
import org.springframework.data.document.mongodb.convert.MongoConverters.ObjectIdToBigIntegerConverter;
import org.springframework.data.document.mongodb.convert.MongoConverters.ObjectIdToStringConverter;
import org.springframework.data.document.mongodb.convert.MongoConverters.StringToBigDecimalConverter;
import org.springframework.data.document.mongodb.convert.MongoConverters.StringToObjectIdConverter;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

View File

@@ -30,6 +30,8 @@ import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.document.mongodb.convert.MongoConverters.BigDecimalToStringConverter;
import org.springframework.data.document.mongodb.convert.MongoConverters.StringToBigDecimalConverter;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.util.Assert;
@@ -76,16 +78,16 @@ public class CustomConversions {
this.writingPairs = new HashSet<ConvertiblePair>();
this.customSimpleTypes = new HashSet<Class<?>>();
registerConversion(CustomToStringConverter.INSTANCE);
for (Object c : converters) {
registerConversion(c);
}
this.converters = new ArrayList<Object>();
this.converters.add(CustomToStringConverter.INSTANCE);
this.converters.add(BigDecimalToStringConverter.INSTANCE);
this.converters.add(StringToBigDecimalConverter.INSTANCE);
this.converters.addAll(converters);
for (Object c : this.converters) {
registerConversion(c);
}
this.simpleTypeHolder = new SimpleTypeHolder(customSimpleTypes, true);
}

View File

@@ -15,22 +15,23 @@
*/
package org.springframework.data.document.mongodb.convert;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.bson.types.ObjectId;
import org.springframework.core.convert.converter.Converter;
/**
* Wrapper class to contain useful {@link ObjectId}-to-something-and-back converters.
* Wrapper class to contain useful converters for the usage with Mongo.
*
* @author Oliver Gierke
*/
abstract class ObjectIdConverters {
abstract class MongoConverters {
/**
* Private constructor to prevent instantiation.
*/
private ObjectIdConverters() {
private MongoConverters() {
}
@@ -85,4 +86,21 @@ abstract class ObjectIdConverters {
return new ObjectId(source.toString(16));
}
}
public static enum BigDecimalToStringConverter implements Converter<BigDecimal, String> {
INSTANCE;
public String convert(BigDecimal source) {
return source.toString();
}
}
public static enum StringToBigDecimalConverter implements Converter<String, BigDecimal> {
INSTANCE;
public BigDecimal convert(String source) {
return new BigDecimal(source);
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.data.document.mongodb.mapping;
package org.springframework.data.document.mongodb.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
@@ -39,6 +39,8 @@ import org.springframework.core.convert.converter.Converter;
import org.springframework.data.document.mongodb.MongoDbFactory;
import org.springframework.data.document.mongodb.convert.CustomConversions;
import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
import org.springframework.data.document.mongodb.mapping.Field;
import org.springframework.data.document.mongodb.mapping.MongoMappingContext;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
@@ -317,7 +319,7 @@ public class MappingMongoConverterUnitTests {
}
/**
* @see DATACMNS-42
* @see DATACMNS-42, DATADOC-171
*/
@Test
public void writesClassWithBigDecimal() {
@@ -328,16 +330,17 @@ public class MappingMongoConverterUnitTests {
DBObject dbObject = new BasicDBObject();
converter.write(container, dbObject);
assertThat(dbObject.get("value"), is((Object) container.value));
assertThat(dbObject.get("value"), is(instanceOf(String.class)));
assertThat((String) dbObject.get("value"), is("2.5"));
}
/**
* @see DATACMNS-42
* @see DATACMNS-42, DATADOC-171
*/
@Test
public void readsClassWithBigDecimal() {
DBObject dbObject = new BasicDBObject("value", 2.5d);
DBObject dbObject = new BasicDBObject("value", "2.5");
BigDecimalContainer result = converter.read(BigDecimalContainer.class, dbObject);
assertThat(result.value, is(BigDecimal.valueOf(2.5d)));
@@ -358,7 +361,6 @@ public class MappingMongoConverterUnitTests {
BasicDBList typedOuterString = (BasicDBList) outerStrings;
assertThat(typedOuterString.size(), is(1));
}
class ClassWithEnumProperty {

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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.document.mongodb.convert;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import org.junit.Test;
import org.springframework.data.document.mongodb.convert.MongoConverters.BigDecimalToStringConverter;
import org.springframework.data.document.mongodb.convert.MongoConverters.StringToBigDecimalConverter;
/**
* Unit tests for {@link MongoConverters}.
*
* @author Oliver Gierke
*/
public class MongoConvertersUnitTests {
@Test
public void convertsBigDecimalToStringAndBackCorrectly() {
BigDecimal bigDecimal = BigDecimal.valueOf(254, 1);
String value = BigDecimalToStringConverter.INSTANCE.convert(bigDecimal);
assertThat(value, is("25.4"));
BigDecimal reference = StringToBigDecimalConverter.INSTANCE.convert(value);
assertThat(reference, is(bigDecimal));
}
}