From 5d6bf7d132e3ac0a9096dd60b5408f826f59b518 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 20 Jun 2015 15:18:22 -0500 Subject: [PATCH 1/2] support BigInteger and BigDecimal --- JSONObject.java | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/JSONObject.java b/JSONObject.java index e28c9cd..6b8ce6b 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -30,6 +30,7 @@ import java.io.Writer; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.math.*; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; @@ -503,6 +504,46 @@ public class JSONObject { + "] is not a Boolean."); } + /** + * Get the BigInteger value associated with a key. + * + * @param key + * A key string. + * @return The numeric value. + * @throws JSONException + * if the key is not found or if the value cannot + * be converted to BigInteger. + */ + public BigInteger getBigInteger(String key) throws JSONException { + Object object = this.get(key); + try { + return new BigInteger(object.toString()); + } catch (Exception e) { + throw new JSONException("JSONObject[" + quote(key) + + "] could not be converted to BigInteger."); + } + } + + /** + * Get the BigDecimal value associated with a key. + * + * @param key + * A key string. + * @return The numeric value. + * @throws JSONException + * if the key is not found or if the value + * cannot be converted to BigDecimal. + */ + public BigDecimal getBigDecimal(String key) throws JSONException { + Object object = this.get(key); + try { + return new BigDecimal(object.toString()); + } catch (Exception e) { + throw new JSONException("JSONObject[" + quote(key) + + "] could not be converted to BigDecimal."); + } + } + /** * Get the double value associated with a key. * @@ -688,6 +729,10 @@ public class JSONObject { Object value = this.opt(key); if (value == null) { this.put(key, 1); + } else if (value instanceof BigInteger) { + this.put(key, ((BigInteger)value).add(BigInteger.ONE)); + } else if (value instanceof BigDecimal) { + this.put(key, ((BigDecimal)value).add(BigDecimal.ONE)); } else if (value instanceof Integer) { this.put(key, (Integer) value + 1); } else if (value instanceof Long) { @@ -843,6 +888,44 @@ public class JSONObject { return this.optDouble(key, Double.NaN); } + /** + * Get an optional BigInteger associated with a key, or the defaultValue if + * there is no such key or if its value is not a number. If the value is a + * string, an attempt will be made to evaluate it as a number. + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return An object which is the value. + */ + public BigInteger optBigInteger(String key, BigInteger defaultValue) { + try { + return this.getBigInteger(key); + } catch (Exception e) { + return defaultValue; + } + } + + /** + * Get an optional BigDecimal associated with a key, or the defaultValue if + * there is no such key or if its value is not a number. If the value is a + * string, an attempt will be made to evaluate it as a number. + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return An object which is the value. + */ + public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) { + try { + return this.getBigDecimal(key); + } catch (Exception e) { + return defaultValue; + } + } + /** * Get an optional double associated with a key, or the defaultValue if * there is no such key or if its value is not a number. If the value is a From b39ccc2a67563cd90421d694bb7d0a7e296f9b99 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 20 Jun 2015 15:20:56 -0500 Subject: [PATCH 2/2] support BigDecimal and BigInteger --- JSONObject.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index 6b8ce6b..14f58b8 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -92,7 +92,7 @@ import java.util.Set; * * * @author JSON.org - * @version 2015-05-05 + * @version 2015-06-20 */ public class JSONObject { /**