22 Commits

Author SHA1 Message Date
Sean Leary
44275e3b3c Merge pull request #175 from douglascrockford/release-20151123
Update readme for 20151123 release
2015-11-24 01:53:19 -06:00
stleary
5f2e77f9dd Update readme with proposed release information 2015-11-21 11:50:31 -06:00
Sean Leary
a109f581c8 Update JSONArray.java
Update version for https://github.com/douglascrockford/JSON-java/pull/153
2015-10-29 18:25:27 -05:00
Sean Leary
33ae025e78 Update JSONObject.java
Update version for https://github.com/douglascrockford/JSON-java/pull/153
2015-10-29 18:24:46 -05:00
Sean Leary
564ad2c2fb Merge pull request #153 from treyerl/master
JSONObject and JSONArray initialization for Map<?,?> and Collection<?>
2015-10-29 18:07:43 -05:00
Sean Leary
dfd19116d3 Merge pull request #168 from douglascrockford/readme-maven-info
Include latest Maven release information
2015-10-29 18:04:31 -05:00
stleary
a07d391eae Include latest Maven release information 2015-10-25 11:05:16 -05:00
Sean Leary
e7f4eb5f67 Set version date to match commit date, convert tabs to spaces. 2015-10-18 11:05:29 -05:00
Sean Leary
b0a9507add Merge pull request #159 from johnjaylward/FixExceptionInheritance
Properly overrides the Exception class.
2015-10-18 11:02:11 -05:00
Sean Leary
6757e04c0a Fix NullPointerException in XML.toString(object, tagName)
Setting version date to match commit date.
2015-10-18 10:23:43 -05:00
Sean Leary
09b6af4712 Merge pull request #160 from johnjaylward/FixXMLNPE
Fixes possible NullPointerException in XML.toString(object, tagName)
2015-10-18 10:21:36 -05:00
John Aylward
637c1fe2b9 updates file dates 2015-10-14 00:48:01 -04:00
John Aylward
1448163981 fixes file date 2015-10-14 00:41:38 -04:00
Lukas Treyer
5ddc515679 removed 6 unnecessary @SuppressWarnings("unchecked") annotations. 2015-10-12 23:52:14 +02:00
John J. Aylward
1b06a802cf makes params final 2015-10-12 14:54:30 -04:00
John J. Aylward
0e13241528 Expands javadoc 2015-10-12 14:53:03 -04:00
John J. Aylward
e239e1967a Allows a custom message to be passed with a cause. 2015-10-12 14:52:17 -04:00
John J. Aylward
ceba8e8c3d Fixes possible NPE 2015-10-12 14:25:18 -04:00
John J. Aylward
4e77383472 Properly overrides the Exception class. 2015-10-12 14:23:05 -04:00
Lukas Treyer
25b5aa7ef2 changed Map<String, ?> method parameters to Map<?,?>
changed Iterator to foreach loop in JSONArray ctor
JSONArray(Collection<?> collection) and JSONObject ctor
JSONObject(Map<?,?> map)
2015-10-11 12:21:12 +02:00
Lukas Treyer
409eb9f292 changed all method signatures containing collections and maps to accept
wildcard generic types, e.g. Collection<?> instead of
Collection<Object>. This was proposed by other pull requests (#111,
#112) already. Consider this commit as merge with #111 and #112.

JSONArray:
	- put(Collection<?> value) {...}
	- put(Map<String, ?> value) {...}
	- put(int index, Collection<?> value) throws JSONException {...}
	- put(int index, Map<String, ?> value) throws JSONException {...}

JSONObject:
	- put(String key, Collection<?> value) throws JSONException {...}
	- put(String key, Map<String, ?> value) throws JSONException {...}


Changed all code affected by new JSONObject and JSONArray constructors:
	
JSONObject:
	- valueToString(Object value) throws JSONException {
		- value instanceof Map
		- value instanceof Collection
	  }
	- wrap(Object object) {
		- value instanceof Map
		- value instanceof Collection
	  }
	- writeValue(Writer writer, Object value,
			 int indentFactor, int indent){
        - value instanceof Map
        - value instanceof Collection
      }
2015-10-11 11:20:08 +02:00
Lukas Treyer
0afd26623c JSONObject and JSONArray initialization:
JSONObject(Map<String, ?> map) allows to initialize the JSONObject with
a Map<String, String>

JSONArray(Collection<?> collection) allows to initialize a JSONArray
with a Collection<JSONObject>
2015-10-04 23:17:30 +02:00
5 changed files with 61 additions and 57 deletions

View File

@@ -76,7 +76,7 @@ import java.util.Map;
* </ul>
*
* @author JSON.org
* @version 2015-07-22
* @version 2015-10-29
*/
public class JSONArray implements Iterable<Object> {
@@ -151,13 +151,12 @@ public class JSONArray implements Iterable<Object> {
* @param collection
* A Collection.
*/
public JSONArray(Collection<Object> collection) {
public JSONArray(Collection<?> collection) {
this.myArrayList = new ArrayList<Object>();
if (collection != null) {
Iterator<Object> iter = collection.iterator();
while (iter.hasNext()) {
this.myArrayList.add(JSONObject.wrap(iter.next()));
}
for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o));
}
}
}
@@ -746,7 +745,7 @@ public class JSONArray implements Iterable<Object> {
* A Collection value.
* @return this.
*/
public JSONArray put(Collection<Object> value) {
public JSONArray put(Collection<?> value) {
this.put(new JSONArray(value));
return this;
}
@@ -799,7 +798,7 @@ public class JSONArray implements Iterable<Object> {
* A Map value.
* @return this.
*/
public JSONArray put(Map<String, Object> value) {
public JSONArray put(Map<?, ?> value) {
this.put(new JSONObject(value));
return this;
}
@@ -848,7 +847,7 @@ public class JSONArray implements Iterable<Object> {
* @throws JSONException
* 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));
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
* 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));
return this;
}

View File

@@ -4,11 +4,11 @@ package org.json;
* The JSONException is thrown by the JSON.org classes when things are amiss.
*
* @author JSON.org
* @version 2014-05-03
* @version 2015-10-18
*/
public class JSONException extends RuntimeException {
/** Serialization ID */
private static final long serialVersionUID = 0;
private Throwable cause;
/**
* Constructs a JSONException with an explanatory message.
@@ -16,28 +16,30 @@ public class JSONException extends RuntimeException {
* @param message
* Detail about the reason for the exception.
*/
public JSONException(String message) {
public JSONException(final String message) {
super(message);
}
/**
* Constructs a new JSONException with the specified cause.
* @param cause The cause.
* Constructs a JSONException with an explanatory message and cause.
*
* @param message
* Detail about the reason for the exception.
* @param cause
* The cause.
*/
public JSONException(Throwable cause) {
super(cause.getMessage());
this.cause = cause;
public JSONException(final String message, final Throwable cause) {
super(message, cause);
}
/**
* Returns the cause of this exception or null if the cause is nonexistent
* or unknown.
*
* @return the cause of this exception or null if the cause is nonexistent
* or unknown.
* Constructs a new JSONException with the specified cause.
*
* @param cause
* The cause.
*/
@Override
public Throwable getCause() {
return this.cause;
public JSONException(final Throwable cause) {
super(cause.getMessage(), cause);
}
}

View File

@@ -92,7 +92,7 @@ import java.util.Set;
* </ul>
*
* @author JSON.org
* @version 2015-07-22
* @version 2015-10-29
*/
public class JSONObject {
/**
@@ -243,15 +243,13 @@ public class JSONObject {
* the JSONObject.
* @throws JSONException
*/
public JSONObject(Map<String, Object> map) {
public JSONObject(Map<?, ?> map) {
this.map = new HashMap<String, Object>();
if (map != null) {
Iterator<Entry<String, Object>> i = map.entrySet().iterator();
while (i.hasNext()) {
Entry<String, Object> entry = i.next();
Object value = entry.getValue();
for (final Entry<?, ?> e : map.entrySet()) {
final Object value = e.getValue();
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.
* @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));
return this;
}
@@ -1268,7 +1266,7 @@ public class JSONObject {
* @return this.
* @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));
return this;
}
@@ -1663,13 +1661,11 @@ public class JSONObject {
return value.toString();
}
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
Map<?, ?> map = (Map<?, ?>) value;
return new JSONObject(map).toString();
}
if (value instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) value;
Collection<?> coll = (Collection<?>) value;
return new JSONArray(coll).toString();
}
if (value.getClass().isArray()) {
@@ -1707,16 +1703,14 @@ public class JSONObject {
}
if (object instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) object;
Collection<?> coll = (Collection<?>) object;
return new JSONArray(coll);
}
if (object.getClass().isArray()) {
return new JSONArray(object);
}
if (object instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) object;
Map<?, ?> map = (Map<?, ?>) object;
return new JSONObject(map);
}
Package objectPackage = object.getClass().getPackage();
@@ -1755,14 +1749,11 @@ public class JSONObject {
} else if (value instanceof JSONArray) {
((JSONArray) value).write(writer, indentFactor, indent);
} else if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
Map<?, ?> map = (Map<?, ?>) value;
new JSONObject(map).write(writer, indentFactor, indent);
} else if (value instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) value;
new JSONArray(coll).write(writer, indentFactor,
indent);
Collection<?> coll = (Collection<?>) value;
new JSONArray(coll).write(writer, indentFactor, indent);
} else if (value.getClass().isArray()) {
new JSONArray(value).write(writer, indentFactor, indent);
} else if (value instanceof Number) {

10
README
View File

@@ -62,3 +62,13 @@ JSONML.java: JSONML provides support for converting between JSONML and XML.
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
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

View File

@@ -1,7 +1,7 @@
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
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,
* and to covert a JSONObject into an XML text.
* @author JSON.org
* @version 2014-05-03
* @version 2015-10-18
*/
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
// where XML is lacking, synthesize an <array> element.
} else {
}
if(object!=null){
if (object.getClass().isArray()) {
object = new JSONArray(object);
}
if (object instanceof JSONArray) {
ja = (JSONArray)object;
length = ja.length();
@@ -479,12 +481,12 @@ public class XML {
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
}
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 + ">";
}
}