Skip to content

Commit 4f8b25d

Browse files
committed
Merge pull request stleary#135 from douglascrockford/big-numbers
Proposed BigInteger, BigDecimal support
2 parents 04eab16 + 71d9ad2 commit 4f8b25d

File tree

2 files changed

+166
-3
lines changed

2 files changed

+166
-3
lines changed

JSONArray.java

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ of this software and associated documentation files (the "Software"), to deal
2828
import java.io.StringWriter;
2929
import java.io.Writer;
3030
import java.lang.reflect.Array;
31+
import java.math.*;
3132
import java.util.ArrayList;
3233
import java.util.Collection;
3334
import java.util.Iterator;
@@ -75,7 +76,7 @@ of this software and associated documentation files (the "Software"), to deal
7576
* </ul>
7677
*
7778
* @author JSON.org
78-
* @version 2015-06-04
79+
* @version 2015-07-06
7980
*/
8081
public class JSONArray implements Iterable<Object> {
8182

@@ -246,6 +247,46 @@ public double getDouble(int index) throws JSONException {
246247
}
247248
}
248249

250+
/**
251+
* Get the BigDecimal value associated with an index.
252+
*
253+
* @param index
254+
* The index must be between 0 and length() - 1.
255+
* @return The value.
256+
* @throws JSONException
257+
* If the key is not found or if the value cannot be converted
258+
* to a BigDecimal.
259+
*/
260+
public BigDecimal getBigDecimal (int index) throws JSONException {
261+
Object object = this.get(index);
262+
try {
263+
return new BigDecimal(object.toString());
264+
} catch (Exception e) {
265+
throw new JSONException("JSONArray[" + index +
266+
"] could not convert to BigDecimal.");
267+
}
268+
}
269+
270+
/**
271+
* Get the BigInteger value associated with an index.
272+
*
273+
* @param index
274+
* The index must be between 0 and length() - 1.
275+
* @return The value.
276+
* @throws JSONException
277+
* If the key is not found or if the value cannot be converted
278+
* to a BigInteger.
279+
*/
280+
public BigInteger getBigInteger (int index) throws JSONException {
281+
Object object = this.get(index);
282+
try {
283+
return new BigInteger(object.toString());
284+
} catch (Exception e) {
285+
throw new JSONException("JSONArray[" + index +
286+
"] could not convert to BigInteger.");
287+
}
288+
}
289+
249290
/**
250291
* Get the int value associated with an index.
251292
*
@@ -490,6 +531,44 @@ public int optInt(int index, int defaultValue) {
490531
}
491532
}
492533

534+
/**
535+
* Get the optional BigInteger value associated with an index. The
536+
* defaultValue is returned if there is no value for the index, or if the
537+
* value is not a number and cannot be converted to a number.
538+
*
539+
* @param index
540+
* The index must be between 0 and length() - 1.
541+
* @param defaultValue
542+
* The default value.
543+
* @return The value.
544+
*/
545+
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
546+
try {
547+
return this.getBigInteger(index);
548+
} catch (Exception e) {
549+
return defaultValue;
550+
}
551+
}
552+
553+
/**
554+
* Get the optional BigDecimal value associated with an index. The
555+
* defaultValue is returned if there is no value for the index, or if the
556+
* value is not a number and cannot be converted to a number.
557+
*
558+
* @param index
559+
* The index must be between 0 and length() - 1.
560+
* @param defaultValue
561+
* The default value.
562+
* @return The value.
563+
*/
564+
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
565+
try {
566+
return this.getBigDecimal(index);
567+
} catch (Exception e) {
568+
return defaultValue;
569+
}
570+
}
571+
493572
/**
494573
* Get the optional JSONArray associated with an index.
495574
*

JSONObject.java

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
import java.lang.reflect.Field;
3131
import java.lang.reflect.Method;
3232
import java.lang.reflect.Modifier;
33+
import java.math.*;
3334
import java.util.Collection;
3435
import java.util.Enumeration;
3536
import java.util.HashMap;
@@ -91,7 +92,7 @@ of this software and associated documentation files (the "Software"), to deal
9192
* </ul>
9293
*
9394
* @author JSON.org
94-
* @version 2015-05-05
95+
* @version 2015-07-06
9596
*/
9697
public class JSONObject {
9798
/**
@@ -503,6 +504,46 @@ public boolean getBoolean(String key) throws JSONException {
503504
+ "] is not a Boolean.");
504505
}
505506

507+
/**
508+
* Get the BigInteger value associated with a key.
509+
*
510+
* @param key
511+
* A key string.
512+
* @return The numeric value.
513+
* @throws JSONException
514+
* if the key is not found or if the value cannot
515+
* be converted to BigInteger.
516+
*/
517+
public BigInteger getBigInteger(String key) throws JSONException {
518+
Object object = this.get(key);
519+
try {
520+
return new BigInteger(object.toString());
521+
} catch (Exception e) {
522+
throw new JSONException("JSONObject[" + quote(key)
523+
+ "] could not be converted to BigInteger.");
524+
}
525+
}
526+
527+
/**
528+
* Get the BigDecimal value associated with a key.
529+
*
530+
* @param key
531+
* A key string.
532+
* @return The numeric value.
533+
* @throws JSONException
534+
* if the key is not found or if the value
535+
* cannot be converted to BigDecimal.
536+
*/
537+
public BigDecimal getBigDecimal(String key) throws JSONException {
538+
Object object = this.get(key);
539+
try {
540+
return new BigDecimal(object.toString());
541+
} catch (Exception e) {
542+
throw new JSONException("JSONObject[" + quote(key)
543+
+ "] could not be converted to BigDecimal.");
544+
}
545+
}
546+
506547
/**
507548
* Get the double value associated with a key.
508549
*
@@ -688,6 +729,10 @@ public JSONObject increment(String key) throws JSONException {
688729
Object value = this.opt(key);
689730
if (value == null) {
690731
this.put(key, 1);
732+
} else if (value instanceof BigInteger) {
733+
this.put(key, ((BigInteger)value).add(BigInteger.ONE));
734+
} else if (value instanceof BigDecimal) {
735+
this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
691736
} else if (value instanceof Integer) {
692737
this.put(key, (Integer) value + 1);
693738
} else if (value instanceof Long) {
@@ -843,6 +888,44 @@ public double optDouble(String key) {
843888
return this.optDouble(key, Double.NaN);
844889
}
845890

891+
/**
892+
* Get an optional BigInteger associated with a key, or the defaultValue if
893+
* there is no such key or if its value is not a number. If the value is a
894+
* string, an attempt will be made to evaluate it as a number.
895+
*
896+
* @param key
897+
* A key string.
898+
* @param defaultValue
899+
* The default.
900+
* @return An object which is the value.
901+
*/
902+
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
903+
try {
904+
return this.getBigInteger(key);
905+
} catch (Exception e) {
906+
return defaultValue;
907+
}
908+
}
909+
910+
/**
911+
* Get an optional BigDecimal associated with a key, or the defaultValue if
912+
* there is no such key or if its value is not a number. If the value is a
913+
* string, an attempt will be made to evaluate it as a number.
914+
*
915+
* @param key
916+
* A key string.
917+
* @param defaultValue
918+
* The default.
919+
* @return An object which is the value.
920+
*/
921+
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
922+
try {
923+
return this.getBigDecimal(key);
924+
} catch (Exception e) {
925+
return defaultValue;
926+
}
927+
}
928+
846929
/**
847930
* Get an optional double associated with a key, or the defaultValue if
848931
* there is no such key or if its value is not a number. If the value is a
@@ -1550,7 +1633,8 @@ public static Object wrap(Object object) {
15501633
|| object instanceof Short || object instanceof Integer
15511634
|| object instanceof Long || object instanceof Boolean
15521635
|| object instanceof Float || object instanceof Double
1553-
|| object instanceof String) {
1636+
|| object instanceof String || object instanceof BigInteger
1637+
|| object instanceof BigDecimal) {
15541638
return object;
15551639
}
15561640

0 commit comments

Comments
 (0)