Skip to content

Commit b3abaa5

Browse files
authored
Merge pull request #1 from stleary/master
update from origin
2 parents 5b67330 + eb569b5 commit b3abaa5

File tree

9 files changed

+509
-63
lines changed

9 files changed

+509
-63
lines changed

CDL.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ of this software and associated documentation files (the "Software"), to deal
4141
* The names for the elements in the JSONObjects can be taken from the names
4242
* in the first row.
4343
* @author JSON.org
44-
* @version 2015-12-09
44+
* @version 2016-05-01
4545
*/
4646
public class CDL {
4747

@@ -69,7 +69,12 @@ private static String getValue(JSONTokener x) throws JSONException {
6969
for (;;) {
7070
c = x.next();
7171
if (c == q) {
72-
break;
72+
//Handle escaped double-quote
73+
if(x.next() != '\"')
74+
{
75+
x.back();
76+
break;
77+
}
7378
}
7479
if (c == 0 || c == '\n' || c == '\r') {
7580
throw x.syntaxError("Missing close quote '" + q + "'.");

JSONArray.java

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ 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.*;
31+
import java.math.BigDecimal;
32+
import java.math.BigInteger;
3233
import java.util.ArrayList;
3334
import java.util.Collection;
3435
import java.util.Iterator;
36+
import java.util.List;
3537
import java.util.Map;
3638

3739
/**
@@ -76,7 +78,7 @@ of this software and associated documentation files (the "Software"), to deal
7678
* </ul>
7779
*
7880
* @author JSON.org
79-
* @version 2015-10-29
81+
* @version 2016-05-20
8082
*/
8183
public class JSONArray implements Iterable<Object> {
8284

@@ -593,7 +595,9 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
593595
return myE;
594596
}
595597
return Enum.valueOf(clazz, val.toString());
596-
} catch (IllegalArgumentException | NullPointerException e) {
598+
} catch (IllegalArgumentException e) {
599+
return defaultValue;
600+
} catch (NullPointerException e) {
597601
return defaultValue;
598602
}
599603
}
@@ -955,6 +959,46 @@ public JSONArray put(int index, Object value) throws JSONException {
955959
}
956960
return this;
957961
}
962+
963+
/**
964+
* Creates a JSONPointer using an intialization string and tries to
965+
* match it to an item within this JSONArray. For example, given a
966+
* JSONArray initialized with this document:
967+
* <pre>
968+
* [
969+
* {"b":"c"}
970+
* ]
971+
* </pre>
972+
* and this JSONPointer string:
973+
* <pre>
974+
* "/0/b"
975+
* </pre>
976+
* Then this method will return the String "c"
977+
* A JSONPointerException may be thrown from code called by this method.
978+
*
979+
* @param jsonPointer string that can be used to create a JSONPointer
980+
* @return the item matched by the JSONPointer, otherwise null
981+
*/
982+
public Object query(String jsonPointer) {
983+
return new JSONPointer(jsonPointer).queryFrom(this);
984+
}
985+
986+
/**
987+
* Queries and returns a value from this object using {@code jsonPointer}, or
988+
* returns null if the query fails due to a missing key.
989+
*
990+
* @param jsonPointer the string representation of the JSON pointer
991+
* @return the queried value or {@code null}
992+
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
993+
*/
994+
public Object optQuery(String jsonPointer) {
995+
JSONPointer pointer = new JSONPointer(jsonPointer);
996+
try {
997+
return pointer.queryFrom(this);
998+
} catch (JSONPointerException e) {
999+
return null;
1000+
}
1001+
}
9581002

9591003
/**
9601004
* Remove an index and close the hole.
@@ -1128,4 +1172,29 @@ public Writer write(Writer writer, int indentFactor, int indent)
11281172
throw new JSONException(e);
11291173
}
11301174
}
1175+
1176+
/**
1177+
* Returns a java.util.List containing all of the elements in this array.
1178+
* If an element in the array is a JSONArray or JSONObject it will also
1179+
* be converted.
1180+
* <p>
1181+
* Warning: This method assumes that the data structure is acyclical.
1182+
*
1183+
* @return a java.util.List containing the elements of this array
1184+
*/
1185+
public List<Object> toList() {
1186+
List<Object> results = new ArrayList<Object>(this.myArrayList.size());
1187+
for (Object element : this.myArrayList) {
1188+
if (element == null || JSONObject.NULL.equals(element)) {
1189+
results.add(null);
1190+
} else if (element instanceof JSONArray) {
1191+
results.add(((JSONArray) element).toList());
1192+
} else if (element instanceof JSONObject) {
1193+
results.add(((JSONObject) element).toMap());
1194+
} else {
1195+
results.add(element);
1196+
}
1197+
}
1198+
return results;
1199+
}
11311200
}

JSONML.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal
3333
* the JsonML transform.
3434
*
3535
* @author JSON.org
36-
* @version 2015-12-09
36+
* @version 2016-01-30
3737
*/
3838
public class JSONML {
3939

@@ -174,7 +174,7 @@ private static Object parse(
174174
if (!(token instanceof String)) {
175175
throw x.syntaxError("Missing value");
176176
}
177-
newjo.accumulate(attribute, XML.stringToValue((String)token));
177+
newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
178178
token = null;
179179
} else {
180180
newjo.accumulate(attribute, "");
@@ -227,7 +227,7 @@ private static Object parse(
227227
} else {
228228
if (ja != null) {
229229
ja.put(token instanceof String
230-
? XML.stringToValue((String)token)
230+
? JSONObject.stringToValue((String)token)
231231
: token);
232232
}
233233
}

JSONObject.java

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ 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.*;
33+
import java.math.BigDecimal;
34+
import java.math.BigInteger;
3435
import java.util.Collection;
3536
import java.util.Enumeration;
3637
import java.util.HashMap;
@@ -92,7 +93,7 @@ of this software and associated documentation files (the "Software"), to deal
9293
* </ul>
9394
*
9495
* @author JSON.org
95-
* @version 2015-12-09
96+
* @version 2016-05-20
9697
*/
9798
public class JSONObject {
9899
/**
@@ -900,7 +901,9 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue)
900901
return myE;
901902
}
902903
return Enum.valueOf(clazz, val.toString());
903-
} catch (IllegalArgumentException | NullPointerException e) {
904+
} catch (IllegalArgumentException e) {
905+
return defaultValue;
906+
} catch (NullPointerException e) {
904907
return defaultValue;
905908
}
906909
}
@@ -1335,6 +1338,46 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
13351338
return this;
13361339
}
13371340

1341+
/**
1342+
* Creates a JSONPointer using an intialization string and tries to
1343+
* match it to an item within this JSONObject. For example, given a
1344+
* JSONObject initialized with this document:
1345+
* <pre>
1346+
* {
1347+
* "a":{"b":"c"}
1348+
* }
1349+
* </pre>
1350+
* and this JSONPointer string:
1351+
* <pre>
1352+
* "/a/b"
1353+
* </pre>
1354+
* Then this method will return the String "c".
1355+
* A JSONPointerException may be thrown from code called by this method.
1356+
*
1357+
* @param jsonPointer string that can be used to create a JSONPointer
1358+
* @return the item matched by the JSONPointer, otherwise null
1359+
*/
1360+
public Object query(String jsonPointer) {
1361+
return new JSONPointer(jsonPointer).queryFrom(this);
1362+
}
1363+
1364+
/**
1365+
* Queries and returns a value from this object using {@code jsonPointer}, or
1366+
* returns null if the query fails due to a missing key.
1367+
*
1368+
* @param jsonPointer the string representation of the JSON pointer
1369+
* @return the queried value or {@code null}
1370+
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
1371+
*/
1372+
public Object optQuery(String jsonPointer) {
1373+
JSONPointer pointer = new JSONPointer(jsonPointer);
1374+
try {
1375+
return pointer.queryFrom(this);
1376+
} catch (JSONPointerException e) {
1377+
return null;
1378+
}
1379+
}
1380+
13381381
/**
13391382
* Produce a string in double quotes with backslash sequences in all the
13401383
* right places. A backslash will be inserted within </, producing <\/,
@@ -1477,7 +1520,6 @@ public boolean similar(Object other) {
14771520
* @return A simple JSON value.
14781521
*/
14791522
public static Object stringToValue(String string) {
1480-
Double d;
14811523
if (string.equals("")) {
14821524
return string;
14831525
}
@@ -1496,23 +1538,23 @@ public static Object stringToValue(String string) {
14961538
* produced, then the value will just be a string.
14971539
*/
14981540

1499-
char b = string.charAt(0);
1500-
if ((b >= '0' && b <= '9') || b == '-') {
1541+
char initial = string.charAt(0);
1542+
if ((initial >= '0' && initial <= '9') || initial == '-') {
15011543
try {
15021544
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
1503-
|| string.indexOf('E') > -1) {
1504-
d = Double.valueOf(string);
1545+
|| string.indexOf('E') > -1
1546+
|| "-0".equals(string)) {
1547+
Double d = Double.valueOf(string);
15051548
if (!d.isInfinite() && !d.isNaN()) {
15061549
return d;
15071550
}
15081551
} else {
15091552
Long myLong = new Long(string);
15101553
if (string.equals(myLong.toString())) {
1511-
if (myLong == myLong.intValue()) {
1512-
return myLong.intValue();
1513-
} else {
1514-
return myLong;
1554+
if (myLong.longValue() == myLong.intValue()) {
1555+
return Integer.valueOf(myLong.intValue());
15151556
}
1557+
return myLong;
15161558
}
15171559
}
15181560
} catch (Exception ignore) {
@@ -1836,4 +1878,31 @@ public Writer write(Writer writer, int indentFactor, int indent)
18361878
throw new JSONException(exception);
18371879
}
18381880
}
1881+
1882+
/**
1883+
* Returns a java.util.Map containing all of the entrys in this object.
1884+
* If an entry in the object is a JSONArray or JSONObject it will also
1885+
* be converted.
1886+
* <p>
1887+
* Warning: This method assumes that the data structure is acyclical.
1888+
*
1889+
* @return a java.util.Map containing the entrys of this object
1890+
*/
1891+
public Map<String, Object> toMap() {
1892+
Map<String, Object> results = new HashMap<String, Object>();
1893+
for (Entry<String, Object> entry : this.map.entrySet()) {
1894+
Object value;
1895+
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
1896+
value = null;
1897+
} else if (entry.getValue() instanceof JSONObject) {
1898+
value = ((JSONObject) entry.getValue()).toMap();
1899+
} else if (entry.getValue() instanceof JSONArray) {
1900+
value = ((JSONArray) entry.getValue()).toList();
1901+
} else {
1902+
value = entry.getValue();
1903+
}
1904+
results.put(entry.getKey(), value);
1905+
}
1906+
return results;
1907+
}
18391908
}

0 commit comments

Comments
 (0)