Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
07b2d65
Fixes #187 -0 now returns as a double.
Jan 27, 2016
39b1c0c
fixes error in -0 check
Jan 27, 2016
3007fc8
Removes custom XML stringToValue method in favor of keeping a consistent
Jan 27, 2016
c2b3f2b
adds back in the XML.stringToValue method, but deprecates it.
Jan 27, 2016
97f1b27
Merge pull request #188 from johnjaylward/FixNegativeZero
stleary Jan 30, 2016
62486fd
Version date
stleary Jan 30, 2016
93c79ca
Version date
stleary Jan 30, 2016
ba2585f
Version date
stleary Jan 30, 2016
60349ec
Java 1.6 compatibility.
skreutzer Feb 9, 2016
07a0f60
Merge pull request #195 from publishing-systems/master
stleary Feb 13, 2016
2657915
Update README
stleary Feb 14, 2016
f024b52
Adds JSONArray toList method and JSONObject toMap method
joeferner Feb 27, 2016
26477f4
Merge pull request #203 from joeferner/to-java
stleary Mar 5, 2016
2f3af56
Update date of merge.
stleary Mar 5, 2016
25a8797
Update date of merge.
stleary Mar 5, 2016
86cbfbc
Added CSV change to CDL.java
brianrussell2 Apr 12, 2016
612e419
initial implementation of JSONPointer
erosb Apr 17, 2016
5bee7e3
escape handling improvements & URL fragment notation handling
erosb Apr 26, 2016
792c6f6
improved failure handling
erosb Apr 26, 2016
45bd72c
added JSONObject#query() and JSONPointer#query() methods
erosb Apr 26, 2016
5223aad
added some javadoc to JSONPointer
erosb Apr 26, 2016
bff3527
removed @author tag from JSONPointerException
erosb Apr 26, 2016
c1a789a
adding missing license headers
erosb Apr 26, 2016
cbb1546
README improvements for stleary/JSON-Java#218
erosb Apr 28, 2016
1ca8933
fix to compile with 1.6.0_45
stleary Apr 29, 2016
f239dc7
Merge pull request #219 from captainIowa/master
stleary May 2, 2016
ebf08f5
Merge pull request #226 from stleary/restore-1.6-compatibility
stleary May 2, 2016
cad2342
Update JSONObject.java
stleary May 3, 2016
f21ffbe
Update CDL.java
stleary May 3, 2016
d833c2d
added builder class for JSONPointer, and implemented toString() and t…
erosb May 3, 2016
5ae6a66
added javadoc & null check in Builder#append(String)
erosb May 3, 2016
c044eb1
added copying to JSONPointer constructor
erosb May 5, 2016
8a72509
Merge pull request #222 from erosb/master
stleary May 14, 2016
4a458a9
add timestamps to recent commits, javadocs for JSONObject, JSONArray …
stleary May 14, 2016
9a81b40
added some JavaDocs
stleary May 14, 2016
56be31e
fixing stleary/JSON-Java#233
erosb May 16, 2016
b2bde1f
Merge pull request #234 from erosb/master
stleary May 21, 2016
8083208
Update JSONArray.java
stleary May 21, 2016
612dafc
Update JSONObject.java
stleary May 21, 2016
dfa651e
Make nextString throw a JSONException instead of a NumberFormatExcept…
madsager Jun 2, 2016
16a86d7
Pass in the throwable that caused the error.
madsager Jun 2, 2016
eb569b5
Merge pull request #236 from madsager/master
stleary Jun 8, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CDL.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ of this software and associated documentation files (the "Software"), to deal
* The names for the elements in the JSONObjects can be taken from the names
* in the first row.
* @author JSON.org
* @version 2015-12-09
* @version 2016-05-01
*/
public class CDL {

Expand Down Expand Up @@ -69,7 +69,12 @@ private static String getValue(JSONTokener x) throws JSONException {
for (;;) {
c = x.next();
if (c == q) {
break;
//Handle escaped double-quote
if(x.next() != '\"')
{
x.back();
break;
}
}
if (c == 0 || c == '\n' || c == '\r') {
throw x.syntaxError("Missing close quote '" + q + "'.");
Expand Down
75 changes: 72 additions & 3 deletions JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ of this software and associated documentation files (the "Software"), to deal
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.math.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -76,7 +78,7 @@ of this software and associated documentation files (the "Software"), to deal
* </ul>
*
* @author JSON.org
* @version 2015-10-29
* @version 2016-05-20
*/
public class JSONArray implements Iterable<Object> {

Expand Down Expand Up @@ -593,7 +595,9 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
return myE;
}
return Enum.valueOf(clazz, val.toString());
} catch (IllegalArgumentException | NullPointerException e) {
} catch (IllegalArgumentException e) {
return defaultValue;
} catch (NullPointerException e) {
return defaultValue;
}
}
Expand Down Expand Up @@ -955,6 +959,46 @@ public JSONArray put(int index, Object value) throws JSONException {
}
return this;
}

/**
* Creates a JSONPointer using an intialization string and tries to
* match it to an item within this JSONArray. For example, given a
* JSONArray initialized with this document:
* <pre>
* [
* {"b":"c"}
* ]
* </pre>
* and this JSONPointer string:
* <pre>
* "/0/b"
* </pre>
* Then this method will return the String "c"
* A JSONPointerException may be thrown from code called by this method.
*
* @param jsonPointer string that can be used to create a JSONPointer
* @return the item matched by the JSONPointer, otherwise null
*/
public Object query(String jsonPointer) {
return new JSONPointer(jsonPointer).queryFrom(this);
}

/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
*
* @param jsonPointer the string representation of the JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
*/
public Object optQuery(String jsonPointer) {
JSONPointer pointer = new JSONPointer(jsonPointer);
try {
return pointer.queryFrom(this);
} catch (JSONPointerException e) {
return null;
}
}

/**
* Remove an index and close the hole.
Expand Down Expand Up @@ -1128,4 +1172,29 @@ public Writer write(Writer writer, int indentFactor, int indent)
throw new JSONException(e);
}
}

/**
* Returns a java.util.List containing all of the elements in this array.
* If an element in the array is a JSONArray or JSONObject it will also
* be converted.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a java.util.List containing the elements of this array
*/
public List<Object> toList() {
List<Object> results = new ArrayList<Object>(this.myArrayList.size());
for (Object element : this.myArrayList) {
if (element == null || JSONObject.NULL.equals(element)) {
results.add(null);
} else if (element instanceof JSONArray) {
results.add(((JSONArray) element).toList());
} else if (element instanceof JSONObject) {
results.add(((JSONObject) element).toMap());
} else {
results.add(element);
}
}
return results;
}
}
6 changes: 3 additions & 3 deletions JSONML.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal
* the JsonML transform.
*
* @author JSON.org
* @version 2015-12-09
* @version 2016-01-30
*/
public class JSONML {

Expand Down Expand Up @@ -174,7 +174,7 @@ private static Object parse(
if (!(token instanceof String)) {
throw x.syntaxError("Missing value");
}
newjo.accumulate(attribute, XML.stringToValue((String)token));
newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
token = null;
} else {
newjo.accumulate(attribute, "");
Expand Down Expand Up @@ -227,7 +227,7 @@ private static Object parse(
} else {
if (ja != null) {
ja.put(token instanceof String
? XML.stringToValue((String)token)
? JSONObject.stringToValue((String)token)
: token);
}
}
Expand Down
93 changes: 81 additions & 12 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ of this software and associated documentation files (the "Software"), to deal
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -92,7 +93,7 @@ of this software and associated documentation files (the "Software"), to deal
* </ul>
*
* @author JSON.org
* @version 2015-12-09
* @version 2016-05-20
*/
public class JSONObject {
/**
Expand Down Expand Up @@ -900,7 +901,9 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue)
return myE;
}
return Enum.valueOf(clazz, val.toString());
} catch (IllegalArgumentException | NullPointerException e) {
} catch (IllegalArgumentException e) {
return defaultValue;
} catch (NullPointerException e) {
return defaultValue;
}
}
Expand Down Expand Up @@ -1335,6 +1338,46 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
return this;
}

/**
* Creates a JSONPointer using an intialization string and tries to
* match it to an item within this JSONObject. For example, given a
* JSONObject initialized with this document:
* <pre>
* {
* "a":{"b":"c"}
* }
* </pre>
* and this JSONPointer string:
* <pre>
* "/a/b"
* </pre>
* Then this method will return the String "c".
* A JSONPointerException may be thrown from code called by this method.
*
* @param jsonPointer string that can be used to create a JSONPointer
* @return the item matched by the JSONPointer, otherwise null
*/
public Object query(String jsonPointer) {
return new JSONPointer(jsonPointer).queryFrom(this);
}

/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
*
* @param jsonPointer the string representation of the JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
*/
public Object optQuery(String jsonPointer) {
JSONPointer pointer = new JSONPointer(jsonPointer);
try {
return pointer.queryFrom(this);
} catch (JSONPointerException e) {
return null;
}
}

/**
* Produce a string in double quotes with backslash sequences in all the
* right places. A backslash will be inserted within </, producing <\/,
Expand Down Expand Up @@ -1477,7 +1520,6 @@ public boolean similar(Object other) {
* @return A simple JSON value.
*/
public static Object stringToValue(String string) {
Double d;
if (string.equals("")) {
return string;
}
Expand All @@ -1496,23 +1538,23 @@ public static Object stringToValue(String string) {
* produced, then the value will just be a string.
*/

char b = string.charAt(0);
if ((b >= '0' && b <= '9') || b == '-') {
char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') {
try {
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|| string.indexOf('E') > -1) {
d = Double.valueOf(string);
|| string.indexOf('E') > -1
|| "-0".equals(string)) {
Double d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) {
return d;
}
} else {
Long myLong = new Long(string);
if (string.equals(myLong.toString())) {
if (myLong == myLong.intValue()) {
return myLong.intValue();
} else {
return myLong;
if (myLong.longValue() == myLong.intValue()) {
return Integer.valueOf(myLong.intValue());
}
return myLong;
}
}
} catch (Exception ignore) {
Expand Down Expand Up @@ -1836,4 +1878,31 @@ public Writer write(Writer writer, int indentFactor, int indent)
throw new JSONException(exception);
}
}

/**
* Returns a java.util.Map containing all of the entrys in this object.
* If an entry in the object is a JSONArray or JSONObject it will also
* be converted.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a java.util.Map containing the entrys of this object
*/
public Map<String, Object> toMap() {
Map<String, Object> results = new HashMap<String, Object>();
for (Entry<String, Object> entry : this.map.entrySet()) {
Object value;
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
value = null;
} else if (entry.getValue() instanceof JSONObject) {
value = ((JSONObject) entry.getValue()).toMap();
} else if (entry.getValue() instanceof JSONArray) {
value = ((JSONArray) entry.getValue()).toList();
} else {
value = entry.getValue();
}
results.put(entry.getKey(), value);
}
return results;
}
}
Loading