mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-24 00:03:17 -05:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44275e3b3c | ||
|
|
5f2e77f9dd | ||
|
|
a109f581c8 | ||
|
|
33ae025e78 | ||
|
|
564ad2c2fb | ||
|
|
dfd19116d3 | ||
|
|
a07d391eae | ||
|
|
e7f4eb5f67 | ||
|
|
b0a9507add | ||
|
|
6757e04c0a | ||
|
|
09b6af4712 | ||
|
|
637c1fe2b9 | ||
|
|
1448163981 | ||
|
|
5ddc515679 | ||
|
|
1b06a802cf | ||
|
|
0e13241528 | ||
|
|
e239e1967a | ||
|
|
ceba8e8c3d | ||
|
|
4e77383472 | ||
|
|
25b5aa7ef2 | ||
|
|
409eb9f292 | ||
|
|
0afd26623c |
@@ -76,7 +76,7 @@ import java.util.Map;
|
|||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2015-07-22
|
* @version 2015-10-29
|
||||||
*/
|
*/
|
||||||
public class JSONArray implements Iterable<Object> {
|
public class JSONArray implements Iterable<Object> {
|
||||||
|
|
||||||
@@ -151,13 +151,12 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* @param collection
|
* @param collection
|
||||||
* A Collection.
|
* A Collection.
|
||||||
*/
|
*/
|
||||||
public JSONArray(Collection<Object> collection) {
|
public JSONArray(Collection<?> collection) {
|
||||||
this.myArrayList = new ArrayList<Object>();
|
this.myArrayList = new ArrayList<Object>();
|
||||||
if (collection != null) {
|
if (collection != null) {
|
||||||
Iterator<Object> iter = collection.iterator();
|
for (Object o: collection){
|
||||||
while (iter.hasNext()) {
|
this.myArrayList.add(JSONObject.wrap(o));
|
||||||
this.myArrayList.add(JSONObject.wrap(iter.next()));
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -746,7 +745,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* A Collection value.
|
* A Collection value.
|
||||||
* @return this.
|
* @return this.
|
||||||
*/
|
*/
|
||||||
public JSONArray put(Collection<Object> value) {
|
public JSONArray put(Collection<?> value) {
|
||||||
this.put(new JSONArray(value));
|
this.put(new JSONArray(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -799,7 +798,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* A Map value.
|
* A Map value.
|
||||||
* @return this.
|
* @return this.
|
||||||
*/
|
*/
|
||||||
public JSONArray put(Map<String, Object> value) {
|
public JSONArray put(Map<?, ?> value) {
|
||||||
this.put(new JSONObject(value));
|
this.put(new JSONObject(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -848,7 +847,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
* If the index is negative or if the value is not finite.
|
* If the index is negative or if the value is not finite.
|
||||||
*/
|
*/
|
||||||
public JSONArray put(int index, Collection<Object> value) throws JSONException {
|
public JSONArray put(int index, Collection<?> value) throws JSONException {
|
||||||
this.put(index, new JSONArray(value));
|
this.put(index, new JSONArray(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -920,7 +919,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* If the index is negative or if the the value is an invalid
|
* If the index is negative or if the the value is an invalid
|
||||||
* number.
|
* number.
|
||||||
*/
|
*/
|
||||||
public JSONArray put(int index, Map<String, Object> value) throws JSONException {
|
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
|
||||||
this.put(index, new JSONObject(value));
|
this.put(index, new JSONObject(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ package org.json;
|
|||||||
* The JSONException is thrown by the JSON.org classes when things are amiss.
|
* The JSONException is thrown by the JSON.org classes when things are amiss.
|
||||||
*
|
*
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2014-05-03
|
* @version 2015-10-18
|
||||||
*/
|
*/
|
||||||
public class JSONException extends RuntimeException {
|
public class JSONException extends RuntimeException {
|
||||||
|
/** Serialization ID */
|
||||||
private static final long serialVersionUID = 0;
|
private static final long serialVersionUID = 0;
|
||||||
private Throwable cause;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a JSONException with an explanatory message.
|
* Constructs a JSONException with an explanatory message.
|
||||||
@@ -16,28 +16,30 @@ public class JSONException extends RuntimeException {
|
|||||||
* @param message
|
* @param message
|
||||||
* Detail about the reason for the exception.
|
* Detail about the reason for the exception.
|
||||||
*/
|
*/
|
||||||
public JSONException(String message) {
|
public JSONException(final String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new JSONException with the specified cause.
|
* Constructs a JSONException with an explanatory message and cause.
|
||||||
* @param cause The cause.
|
*
|
||||||
|
* @param message
|
||||||
|
* Detail about the reason for the exception.
|
||||||
|
* @param cause
|
||||||
|
* The cause.
|
||||||
*/
|
*/
|
||||||
public JSONException(Throwable cause) {
|
public JSONException(final String message, final Throwable cause) {
|
||||||
super(cause.getMessage());
|
super(message, cause);
|
||||||
this.cause = cause;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the cause of this exception or null if the cause is nonexistent
|
* Constructs a new JSONException with the specified cause.
|
||||||
* or unknown.
|
*
|
||||||
*
|
* @param cause
|
||||||
* @return the cause of this exception or null if the cause is nonexistent
|
* The cause.
|
||||||
* or unknown.
|
|
||||||
*/
|
*/
|
||||||
@Override
|
public JSONException(final Throwable cause) {
|
||||||
public Throwable getCause() {
|
super(cause.getMessage(), cause);
|
||||||
return this.cause;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ import java.util.Set;
|
|||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2015-07-22
|
* @version 2015-10-29
|
||||||
*/
|
*/
|
||||||
public class JSONObject {
|
public class JSONObject {
|
||||||
/**
|
/**
|
||||||
@@ -243,15 +243,13 @@ public class JSONObject {
|
|||||||
* the JSONObject.
|
* the JSONObject.
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public JSONObject(Map<String, Object> map) {
|
public JSONObject(Map<?, ?> map) {
|
||||||
this.map = new HashMap<String, Object>();
|
this.map = new HashMap<String, Object>();
|
||||||
if (map != null) {
|
if (map != null) {
|
||||||
Iterator<Entry<String, Object>> i = map.entrySet().iterator();
|
for (final Entry<?, ?> e : map.entrySet()) {
|
||||||
while (i.hasNext()) {
|
final Object value = e.getValue();
|
||||||
Entry<String, Object> entry = i.next();
|
|
||||||
Object value = entry.getValue();
|
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
this.map.put(entry.getKey(), wrap(value));
|
this.map.put(String.valueOf(e.getKey()), wrap(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1204,7 +1202,7 @@ public class JSONObject {
|
|||||||
* @return this.
|
* @return this.
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public JSONObject put(String key, Collection<Object> value) throws JSONException {
|
public JSONObject put(String key, Collection<?> value) throws JSONException {
|
||||||
this.put(key, new JSONArray(value));
|
this.put(key, new JSONArray(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -1268,7 +1266,7 @@ public class JSONObject {
|
|||||||
* @return this.
|
* @return this.
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public JSONObject put(String key, Map<String, Object> value) throws JSONException {
|
public JSONObject put(String key, Map<?, ?> value) throws JSONException {
|
||||||
this.put(key, new JSONObject(value));
|
this.put(key, new JSONObject(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -1663,13 +1661,11 @@ public class JSONObject {
|
|||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
if (value instanceof Map) {
|
if (value instanceof Map) {
|
||||||
@SuppressWarnings("unchecked")
|
Map<?, ?> map = (Map<?, ?>) value;
|
||||||
Map<String, Object> map = (Map<String, Object>) value;
|
|
||||||
return new JSONObject(map).toString();
|
return new JSONObject(map).toString();
|
||||||
}
|
}
|
||||||
if (value instanceof Collection) {
|
if (value instanceof Collection) {
|
||||||
@SuppressWarnings("unchecked")
|
Collection<?> coll = (Collection<?>) value;
|
||||||
Collection<Object> coll = (Collection<Object>) value;
|
|
||||||
return new JSONArray(coll).toString();
|
return new JSONArray(coll).toString();
|
||||||
}
|
}
|
||||||
if (value.getClass().isArray()) {
|
if (value.getClass().isArray()) {
|
||||||
@@ -1707,16 +1703,14 @@ public class JSONObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (object instanceof Collection) {
|
if (object instanceof Collection) {
|
||||||
@SuppressWarnings("unchecked")
|
Collection<?> coll = (Collection<?>) object;
|
||||||
Collection<Object> coll = (Collection<Object>) object;
|
|
||||||
return new JSONArray(coll);
|
return new JSONArray(coll);
|
||||||
}
|
}
|
||||||
if (object.getClass().isArray()) {
|
if (object.getClass().isArray()) {
|
||||||
return new JSONArray(object);
|
return new JSONArray(object);
|
||||||
}
|
}
|
||||||
if (object instanceof Map) {
|
if (object instanceof Map) {
|
||||||
@SuppressWarnings("unchecked")
|
Map<?, ?> map = (Map<?, ?>) object;
|
||||||
Map<String, Object> map = (Map<String, Object>) object;
|
|
||||||
return new JSONObject(map);
|
return new JSONObject(map);
|
||||||
}
|
}
|
||||||
Package objectPackage = object.getClass().getPackage();
|
Package objectPackage = object.getClass().getPackage();
|
||||||
@@ -1755,14 +1749,11 @@ public class JSONObject {
|
|||||||
} else if (value instanceof JSONArray) {
|
} else if (value instanceof JSONArray) {
|
||||||
((JSONArray) value).write(writer, indentFactor, indent);
|
((JSONArray) value).write(writer, indentFactor, indent);
|
||||||
} else if (value instanceof Map) {
|
} else if (value instanceof Map) {
|
||||||
@SuppressWarnings("unchecked")
|
Map<?, ?> map = (Map<?, ?>) value;
|
||||||
Map<String, Object> map = (Map<String, Object>) value;
|
|
||||||
new JSONObject(map).write(writer, indentFactor, indent);
|
new JSONObject(map).write(writer, indentFactor, indent);
|
||||||
} else if (value instanceof Collection) {
|
} else if (value instanceof Collection) {
|
||||||
@SuppressWarnings("unchecked")
|
Collection<?> coll = (Collection<?>) value;
|
||||||
Collection<Object> coll = (Collection<Object>) value;
|
new JSONArray(coll).write(writer, indentFactor, indent);
|
||||||
new JSONArray(coll).write(writer, indentFactor,
|
|
||||||
indent);
|
|
||||||
} else if (value.getClass().isArray()) {
|
} else if (value.getClass().isArray()) {
|
||||||
new JSONArray(value).write(writer, indentFactor, indent);
|
new JSONArray(value).write(writer, indentFactor, indent);
|
||||||
} else if (value instanceof Number) {
|
} else if (value instanceof Number) {
|
||||||
|
|||||||
10
README
10
README
@@ -62,3 +62,13 @@ JSONML.java: JSONML provides support for converting between JSONML and XML.
|
|||||||
XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text.
|
XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text.
|
||||||
|
|
||||||
Unit tests are maintained in a separate project. Contributing developers can test JSON-java pull requests with the code in this project: https://github.com/stleary/JSON-Java-unit-test
|
Unit tests are maintained in a separate project. Contributing developers can test JSON-java pull requests with the code in this project: https://github.com/stleary/JSON-Java-unit-test
|
||||||
|
|
||||||
|
Release history:
|
||||||
|
|
||||||
|
20151123 JSONObject and JSONArray initialization with generics. Contains the
|
||||||
|
latest code as of 23 Nov, 2015.
|
||||||
|
|
||||||
|
20150729 Checkpoint for Maven central repository release. Contains the latest code as of 29 July, 2015.
|
||||||
|
|
||||||
|
JSON-java releases can be found by searching the Maven repository for groupId "org.json" and artifactId "json". For example:
|
||||||
|
https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22
|
||||||
|
|||||||
18
XML.java
18
XML.java
@@ -1,7 +1,7 @@
|
|||||||
package org.json;
|
package org.json;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Copyright (c) 2002 JSON.org
|
Copyright (c) 2015 JSON.org
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -30,7 +30,7 @@ import java.util.Iterator;
|
|||||||
* This provides static methods to convert an XML text into a JSONObject,
|
* This provides static methods to convert an XML text into a JSONObject,
|
||||||
* and to covert a JSONObject into an XML text.
|
* and to covert a JSONObject into an XML text.
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2014-05-03
|
* @version 2015-10-18
|
||||||
*/
|
*/
|
||||||
public class XML {
|
public class XML {
|
||||||
|
|
||||||
@@ -468,10 +468,12 @@ public class XML {
|
|||||||
// XML does not have good support for arrays. If an array appears in a place
|
// XML does not have good support for arrays. If an array appears in a place
|
||||||
// where XML is lacking, synthesize an <array> element.
|
// where XML is lacking, synthesize an <array> element.
|
||||||
|
|
||||||
} else {
|
}
|
||||||
|
if(object!=null){
|
||||||
if (object.getClass().isArray()) {
|
if (object.getClass().isArray()) {
|
||||||
object = new JSONArray(object);
|
object = new JSONArray(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (object instanceof JSONArray) {
|
if (object instanceof JSONArray) {
|
||||||
ja = (JSONArray)object;
|
ja = (JSONArray)object;
|
||||||
length = ja.length();
|
length = ja.length();
|
||||||
@@ -479,12 +481,12 @@ public class XML {
|
|||||||
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
|
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
} else {
|
|
||||||
string = (object == null) ? "null" : escape(object.toString());
|
|
||||||
return (tagName == null) ? "\"" + string + "\"" :
|
|
||||||
(string.length() == 0) ? "<" + tagName + "/>" :
|
|
||||||
"<" + tagName + ">" + string + "</" + tagName + ">";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
string = (object == null) ? "null" : escape(object.toString());
|
||||||
|
return (tagName == null) ? "\"" + string + "\"" :
|
||||||
|
(string.length() == 0) ? "<" + tagName + "/>" :
|
||||||
|
"<" + tagName + ">" + string + "</" + tagName + ">";
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user