From 8bc62cc34c071a73c339a878b116bcb1f80d69d2 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 20 Jun 2015 13:26:55 -0500 Subject: [PATCH 1/7] support for 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 5d6bf7d132e3ac0a9096dd60b5408f826f59b518 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 20 Jun 2015 15:18:22 -0500 Subject: [PATCH 2/7] 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 3/7] 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 { /** From a76d7262d11e34c9adb403d76c98db21e0c83bb6 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 20 Jun 2015 16:20:00 -0500 Subject: [PATCH 4/7] support BigInteger and BigDecimal --- JSONObject.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index 14f58b8..682c513 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -1633,7 +1633,8 @@ public class JSONObject { || object instanceof Short || object instanceof Integer || object instanceof Long || object instanceof Boolean || object instanceof Float || object instanceof Double - || object instanceof String) { + || object instanceof String || object instanceof BigInteger + || object instanceof BigDecimal) { return object; } From 6ab6f063c82e1604f2a5af59d4f4a072165ab55b Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 3 Jul 2015 20:41:47 -0500 Subject: [PATCH 5/7] latest --- JSONArray.java | 43 ++++++++++++++++++++++++++++++++++++++++++- JSONObject.java | 2 +- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index deede03..22d4ab7 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Array; +import java.math.*; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -75,7 +76,7 @@ import java.util.Map; * * * @author JSON.org - * @version 2015-06-04 + * @version 2015-07-04 */ public class JSONArray implements Iterable { @@ -246,6 +247,46 @@ public class JSONArray implements Iterable { } } + /** + * Get the BigDecimal value associated with an index. + * + * @param index + * The index must be between 0 and length() - 1. + * @return The value. + * @throws JSONException + * If the key is not found or if the value cannot be converted + * to a BigDecimal. + */ + public BigDecimal getBigDecimal (int index) throws JSONException { + Object object = this.get(index); + try { + return new BigDecimal(object.toString()); + } catch (Exception e) { + throw new JSONException("JSONArray[" + index + + "] could not convert to BigDecimal."); + } + } + + /** + * Get the BigInteger value associated with an index. + * + * @param index + * The index must be between 0 and length() - 1. + * @return The value. + * @throws JSONException + * If the key is not found or if the value cannot be converted + * to a BigInteger. + */ + public BigInteger getBigInteger (int index) throws JSONException { + Object object = this.get(index); + try { + return new BigInteger(object.toString()); + } catch (Exception e) { + throw new JSONException("JSONArray[" + index + + "] could not convert to BigInteger."); + } + } + /** * Get the int value associated with an index. * diff --git a/JSONObject.java b/JSONObject.java index 14f58b8..24223c5 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -92,7 +92,7 @@ import java.util.Set; * * * @author JSON.org - * @version 2015-06-20 + * @version 2015-07-04 */ public class JSONObject { /** From 410afaff145ba59c4979c0492e2d244ac6e6dfe4 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 6 Jul 2015 22:20:19 -0500 Subject: [PATCH 6/7] latest --- JSONArray.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/JSONArray.java b/JSONArray.java index 22d4ab7..684ed7a 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -531,6 +531,44 @@ public class JSONArray implements Iterable { } } + /** + * Get the optional BigInteger value associated with an index. The + * defaultValue is returned if there is no value for the index, or if the + * value is not a number and cannot be converted to a number. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default value. + * @return The value. + */ + public BigInteger optBigInteger(int index, BigInteger defaultValue) { + try { + return this.getBigInteger(index); + } catch (Exception e) { + return defaultValue; + } + } + + /** + * Get the optional BigDecimal value associated with an index. The + * defaultValue is returned if there is no value for the index, or if the + * value is not a number and cannot be converted to a number. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default value. + * @return The value. + */ + public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) { + try { + return this.getBigDecimal(index); + } catch (Exception e) { + return defaultValue; + } + } + /** * Get the optional JSONArray associated with an index. * From 71d9ad2b9909fa5d7fe6fcc85879002628ea9de4 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 6 Jul 2015 22:27:10 -0500 Subject: [PATCH 7/7] update version dates --- JSONArray.java | 2 +- JSONObject.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 684ed7a..b1334db 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -76,7 +76,7 @@ import java.util.Map; * * * @author JSON.org - * @version 2015-07-04 + * @version 2015-07-06 */ public class JSONArray implements Iterable { diff --git a/JSONObject.java b/JSONObject.java index ff3f900..34d5c6c 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -92,7 +92,7 @@ import java.util.Set; * * * @author JSON.org - * @version 2015-07-04 + * @version 2015-07-06 */ public class JSONObject { /**