prepare issue 40: do not create Double for BigDecimal to keep high precision
diff --git a/src/main/java/org/yaml/snakeyaml/constructor/Constructor.java b/src/main/java/org/yaml/snakeyaml/constructor/Constructor.java
index e14d239..81a1da9 100644
--- a/src/main/java/org/yaml/snakeyaml/constructor/Constructor.java
+++ b/src/main/java/org/yaml/snakeyaml/constructor/Constructor.java
@@ -238,6 +238,10 @@
try {
Property property = getProperty(beanType, key);
valueNode.setType(property.getType());
+ //TODO remove BigDecimal.class
+ if (BigDecimal.class.equals(property.getType())) {
+ valueNode.setUseClassConstructor(true);
+ }
if (property.getType().isArray()) {
isArray = true;
}
@@ -398,7 +402,8 @@
if (type.isPrimitive() || type == String.class || Number.class.isAssignableFrom(type)
|| type == Boolean.class || Date.class.isAssignableFrom(type)
|| type == Character.class || type == BigInteger.class
- || Enum.class.isAssignableFrom(type) || Tags.BINARY.equals(node.getTag())) {
+ || type == BigDecimal.class || Enum.class.isAssignableFrom(type)
+ || Tags.BINARY.equals(node.getTag())) {
// standard classes created directly
result = constructStandardJavaInstance(type, node);
} else {
@@ -481,12 +486,14 @@
}
} else if (type == Float.class || type == Double.class || type == Float.TYPE
|| type == Double.TYPE || type == BigDecimal.class) {
- Construct doubleConstructor = yamlConstructors.get(Tags.FLOAT);
- result = doubleConstructor.construct(node);
- if (type == Float.class || type == Float.TYPE) {
- result = new Float((Double) result);
- } else if (type == BigDecimal.class) {
- result = new BigDecimal(((Double) result).doubleValue());
+ if (type == BigDecimal.class) {
+ result = new BigDecimal(node.getValue());
+ } else {
+ Construct doubleConstructor = yamlConstructors.get(Tags.FLOAT);
+ result = doubleConstructor.construct(node);
+ if (type == Float.class || type == Float.TYPE) {
+ result = new Float((Double) result);
+ }
}
} else if (type == Byte.class || type == Short.class || type == Integer.class
|| type == Long.class || type == BigInteger.class || type == Byte.TYPE
diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBean.java
new file mode 100644
index 0000000..cff20c6
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBean.java
@@ -0,0 +1,19 @@
+package org.yaml.snakeyaml.issues.issue40;
+
+import java.math.BigDecimal;
+
+public class DogFoodBean {
+ BigDecimal decimal;
+
+ public DogFoodBean() {
+ decimal = BigDecimal.ZERO;
+ }
+
+ public BigDecimal getDecimal() {
+ return decimal;
+ }
+
+ public void setDecimal(BigDecimal decimal) {
+ this.decimal = decimal;
+ }
+}
diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBeanTest.java
new file mode 100644
index 0000000..8d7c0d6
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBeanTest.java
@@ -0,0 +1,45 @@
+package org.yaml.snakeyaml.issues.issue40;
+
+import java.math.BigDecimal;
+
+import junit.framework.TestCase;
+
+import org.yaml.snakeyaml.Yaml;
+
+public class DogFoodBeanTest extends TestCase {
+
+ public void testOwnBigDecimal() {
+ DogFoodBean input = new DogFoodBean();
+ input.setDecimal(new BigDecimal("5"));
+ Yaml yaml = new Yaml();
+ String text = yaml.dump(input);
+ System.out.println(text);
+ assertEquals("!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: !!float '5'}\n",
+ text);
+ DogFoodBean output = (DogFoodBean) yaml.load(text);
+ assertEquals(output.getDecimal(), input.getDecimal());
+ }
+
+ public void testBigDecimalPrecision() {
+ DogFoodBean input = new DogFoodBean();
+ input.setDecimal(new BigDecimal("5.123"));
+ Yaml yaml = new Yaml();
+ String text = yaml.dump(input);
+ // System.out.println(text);
+ assertEquals("!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: 5.123}\n", text);
+ DogFoodBean output = (DogFoodBean) yaml.load(text);
+ assertEquals(input.getDecimal(), output.getDecimal());
+ }
+
+ public void testBigDecimal1() {
+ Yaml yaml = new Yaml();
+ String text = yaml.dump(new BigDecimal("5"));
+ assertEquals("!!float '5'\n", text);
+ }
+
+ public void testBigDecimal2() {
+ Yaml yaml = new Yaml();
+ String text = yaml.dump(new BigDecimal("5.123"));
+ assertEquals("5.123\n", text);
+ }
+}