|
| 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