Skip to content

Commit 81fc3c9

Browse files
committed
code.google.com
1 parent c317b3a commit 81fc3c9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

json-smart/src/main/java/net/minidev/json/reader/JsonWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.minidev.json.reader;
22

33
import java.io.IOException;
4+
import java.math.BigDecimal;
45
import java.math.BigInteger;
56
import java.util.Date;
67
import java.util.LinkedList;
@@ -204,7 +205,7 @@ public void writeJSONString(Float value, Appendable out, JSONStyle compression)
204205
public void writeJSONString(Number value, Appendable out, JSONStyle compression) throws IOException {
205206
out.append(value.toString());
206207
}
207-
}, Integer.class, Long.class, Byte.class, Short.class, BigInteger.class);
208+
}, Integer.class, Long.class, Byte.class, Short.class, BigInteger.class, BigDecimal.class);
208209

209210
registerWriter(new JsonWriterI<Boolean>() {
210211
public void writeJSONString(Boolean value, Appendable out, JSONStyle compression) throws IOException {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.minidev.json.test;
2+
3+
import java.math.BigDecimal;
4+
import java.math.BigInteger;
5+
import java.util.HashMap;
6+
7+
import net.minidev.json.JSONObject;
8+
import net.minidev.json.JSONValue;
9+
import junit.framework.TestCase;
10+
11+
public class TestBigValue extends TestCase {
12+
String bigStr = "12345678901234567890123456789";
13+
14+
/**
15+
* test BigDecimal serialization
16+
*/
17+
public void testBigDecimal() {
18+
HashMap<String, Object> map = new HashMap<String, Object>();
19+
BigDecimal bigDec = new BigDecimal(bigStr + "." + bigStr);
20+
map.put("big", bigDec);
21+
String test = JSONValue.toJSONString(map);
22+
String result = "{\"big\":" + bigStr + "." +bigStr + "}";
23+
assertEquals(result, test);
24+
JSONObject obj = (JSONObject)JSONValue.parse(test);
25+
assertEquals(bigDec, obj.get("big"));
26+
assertEquals(bigDec.getClass(), obj.get("big").getClass());
27+
}
28+
29+
/**
30+
* test BigInteger serialization
31+
*/
32+
public void testBigInteger() {
33+
HashMap<String, Object> map = new HashMap<String, Object>();
34+
BigInteger bigInt = new BigInteger(bigStr);
35+
map.put("big", bigInt);
36+
String test = JSONValue.toJSONString(map);
37+
String result = "{\"big\":" + bigStr + "}";
38+
assertEquals(result, test);
39+
JSONObject obj = (JSONObject)JSONValue.parse(test);
40+
assertEquals(bigInt, obj.get("big"));
41+
assertEquals(bigInt.getClass(), obj.get("big").getClass());
42+
}
43+
}

0 commit comments

Comments
 (0)