Add support for BigDecimal as a JavaBean property
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a83028b..878956d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -7,6 +7,9 @@
</properties>
<body>
<release version="1.3-SNAPSHOT" date="in Mercurial" description="development">
+ <action dev="py4fun" type="add">
+ Add support for BigDecimal as a JavaBean property (2009-07-07)
+ </action>
<action dev="py4fun" type="update">
Improve test coverage for Constructor. Allow construction of JavaBeans
with only setter without the corresponding getter (2009-07-07)
diff --git a/src/main/java/org/yaml/snakeyaml/constructor/Constructor.java b/src/main/java/org/yaml/snakeyaml/constructor/Constructor.java
index 5c636d2..c09949f 100644
--- a/src/main/java/org/yaml/snakeyaml/constructor/Constructor.java
+++ b/src/main/java/org/yaml/snakeyaml/constructor/Constructor.java
@@ -9,6 +9,7 @@
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
@@ -208,13 +209,16 @@
}
}
} else if (type == Float.class || type == Double.class || type == Float.TYPE
- || type == Double.TYPE) {
+ || type == Double.TYPE || type == BigDecimal.class) {
Construct doubleContructor = yamlConstructors.get("tag:yaml.org,2002:float");
result = doubleContructor.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());
}
- } else if (Number.class.isAssignableFrom(type) || type == Byte.TYPE
+ } else if (type == Byte.class || type == Short.class || type == Integer.class
+ || type == Long.class || type == BigInteger.class || type == Byte.TYPE
|| type == Short.TYPE || type == Integer.TYPE || type == Long.TYPE) {
Construct intContructor = yamlConstructors.get("tag:yaml.org,2002:int");
result = intContructor.construct(node);
diff --git a/src/test/java/org/yaml/snakeyaml/constructor/BigDecimalBeanConstructorTest.java b/src/test/java/org/yaml/snakeyaml/constructor/BigDecimalBeanConstructorTest.java
new file mode 100644
index 0000000..072aa11
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/constructor/BigDecimalBeanConstructorTest.java
@@ -0,0 +1,35 @@
+/*
+ * See LICENSE file in distribution for copyright and licensing information.
+ */
+package org.yaml.snakeyaml.constructor;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+
+import junit.framework.TestCase;
+
+import org.yaml.snakeyaml.Yaml;
+
+public class BigDecimalBeanConstructorTest extends TestCase {
+
+ public void testRepresentor() throws IOException {
+ BigDecimalJavaBean bean = new BigDecimalJavaBean();
+ bean.setAmount(1.5f);
+ bean.setNumber(new BigDecimal("3.1416"));
+ Yaml yaml = new Yaml();
+ String output = yaml.dump(bean);
+ String className = this.getClass().getPackage().getName();
+ assertEquals("!!" + className + ".BigDecimalJavaBean {amount: 1.5, number: 3.1416}\n",
+ output);
+ }
+
+ public void testConstructor() throws IOException {
+ String className = "!!" + this.getClass().getPackage().getName()
+ + ".BigDecimalJavaBean {amount: 1.5, number: 3.1416}";
+ Yaml yaml = new Yaml();
+ BigDecimalJavaBean bean = (BigDecimalJavaBean) yaml.load(className);
+ assertNotNull(bean);
+ assertTrue(1.5 - bean.getAmount() < 0.0000001);
+ assertTrue((new BigDecimal("3.1416")).add(bean.getNumber().negate()).doubleValue() < 0.0000001);
+ }
+}
diff --git a/src/test/java/org/yaml/snakeyaml/constructor/BigDecimalJavaBean.java b/src/test/java/org/yaml/snakeyaml/constructor/BigDecimalJavaBean.java
new file mode 100644
index 0000000..37399d9
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/constructor/BigDecimalJavaBean.java
@@ -0,0 +1,27 @@
+/*
+ * See LICENSE file in distribution for copyright and licensing information.
+ */
+package org.yaml.snakeyaml.constructor;
+
+import java.math.BigDecimal;
+
+public class BigDecimalJavaBean {
+ private BigDecimal number;
+ private float amount;
+
+ public BigDecimal getNumber() {
+ return number;
+ }
+
+ public void setNumber(BigDecimal number) {
+ this.number = number;
+ }
+
+ public float getAmount() {
+ return amount;
+ }
+
+ public void setAmount(float amount) {
+ this.amount = amount;
+ }
+}
\ No newline at end of file