Skip to content

Commit e09b862

Browse files
committed
Have JSONArray and JSONObject be Serializable
1 parent 7a17ae0 commit e09b862

6 files changed

Lines changed: 84 additions & 63 deletions

File tree

Cookie.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ of this software and associated documentation files (the "Software"), to deal
2424
SOFTWARE.
2525
*/
2626

27+
import java.io.Serializable;
28+
2729
/**
2830
* Convert a web browser cookie specification to a JSONObject and back.
2931
* JSON and Cookies are both notations for name/value pairs.
@@ -81,7 +83,7 @@ public static String escape(String string) {
8183
public static JSONObject toJSONObject(String string) throws JSONException {
8284
String name;
8385
JSONObject jo = new JSONObject();
84-
Object value;
86+
Serializable value;
8587
JSONTokener x = new JSONTokener(string);
8688
jo.put("name", x.nextTo('='));
8789
x.next('=');

JSONArray.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
2525
*/
2626

2727
import java.io.IOException;
28+
import java.io.Serializable;
2829
import java.io.StringWriter;
2930
import java.io.Writer;
3031
import java.lang.reflect.Array;
@@ -81,18 +82,18 @@ of this software and associated documentation files (the "Software"), to deal
8182
* @author JSON.org
8283
* @version 2016-08/15
8384
*/
84-
public class JSONArray implements Iterable<Object> {
85+
public class JSONArray implements Iterable<Serializable>, Serializable {
8586

8687
/**
8788
* The arrayList where the JSONArray's properties are kept.
8889
*/
89-
private final ArrayList<Object> myArrayList;
90+
private final ArrayList<Serializable> myArrayList;
9091

9192
/**
9293
* Construct an empty JSONArray.
9394
*/
9495
public JSONArray() {
95-
this.myArrayList = new ArrayList<Object>();
96+
this.myArrayList = new ArrayList<Serializable>();
9697
}
9798

9899
/**
@@ -168,12 +169,12 @@ public JSONArray(String source) throws JSONException {
168169
* @param collection
169170
* A Collection.
170171
*/
171-
public JSONArray(Collection<?> collection) {
172+
public JSONArray(Collection<? extends Serializable> collection) {
172173
if (collection == null) {
173-
this.myArrayList = new ArrayList<Object>();
174+
this.myArrayList = new ArrayList<Serializable>();
174175
} else {
175-
this.myArrayList = new ArrayList<Object>(collection.size());
176-
for (Object o: collection){
176+
this.myArrayList = new ArrayList<Serializable>(collection.size());
177+
for (Serializable o: collection){
177178
this.myArrayList.add(JSONObject.wrap(o));
178179
}
179180
}
@@ -188,10 +189,16 @@ public JSONArray(Collection<?> collection) {
188189
public JSONArray(Object array) throws JSONException {
189190
this();
190191
if (array.getClass().isArray()) {
191-
int length = Array.getLength(array);
192-
this.myArrayList.ensureCapacity(length);
193-
for (int i = 0; i < length; i += 1) {
194-
this.put(JSONObject.wrap(Array.get(array, i)));
192+
Class<?> componentType = array.getClass().getComponentType();
193+
if (Serializable.class.isAssignableFrom(componentType)) {
194+
int length = Array.getLength(array);
195+
this.myArrayList.ensureCapacity(length);
196+
for (int i = 0; i < length; i += 1) {
197+
this.put(JSONObject.wrap((Serializable)Array.get(array, i)));
198+
}
199+
} else {
200+
throw new JSONException(
201+
"JSONArray initial value should be serializable.");
195202
}
196203
} else {
197204
throw new JSONException(
@@ -200,7 +207,7 @@ public JSONArray(Object array) throws JSONException {
200207
}
201208

202209
@Override
203-
public Iterator<Object> iterator() {
210+
public Iterator<Serializable> iterator() {
204211
return this.myArrayList.iterator();
205212
}
206213

@@ -213,8 +220,8 @@ public Iterator<Object> iterator() {
213220
* @throws JSONException
214221
* If there is no value for the index.
215222
*/
216-
public Object get(int index) throws JSONException {
217-
Object object = this.opt(index);
223+
public Serializable get(int index) throws JSONException {
224+
Serializable object = this.opt(index);
218225
if (object == null) {
219226
throw new JSONException("JSONArray[" + index + "] not found.");
220227
}
@@ -516,7 +523,7 @@ public int length() {
516523
* The index must be between 0 and length() - 1. If not, null is returned.
517524
* @return An object value, or null if there is no object at that index.
518525
*/
519-
public Object opt(int index) {
526+
public Serializable opt(int index) {
520527
return (index < 0 || index >= this.length()) ? null : this.myArrayList
521528
.get(index);
522529
}
@@ -1031,7 +1038,7 @@ public JSONArray put(long value) {
10311038
* @throws NullPointerException
10321039
* If a key in the map is <code>null</code>
10331040
*/
1034-
public JSONArray put(Map<?, ?> value) {
1041+
public JSONArray put(Map<?, ? extends Serializable> value) {
10351042
return this.put(new JSONObject(value));
10361043
}
10371044

@@ -1046,7 +1053,7 @@ public JSONArray put(Map<?, ?> value) {
10461053
* @throws JSONException
10471054
* If the value is non-finite number.
10481055
*/
1049-
public JSONArray put(Object value) {
1056+
public JSONArray put(Serializable value) {
10501057
JSONObject.testValidity(value);
10511058
this.myArrayList.add(value);
10521059
return this;
@@ -1189,7 +1196,7 @@ public JSONArray put(int index, Map<?, ?> value) throws JSONException {
11891196
* If the index is negative or if the the value is an invalid
11901197
* number.
11911198
*/
1192-
public JSONArray put(int index, Object value) throws JSONException {
1199+
public JSONArray put(int index, Serializable value) throws JSONException {
11931200
if (index < 0) {
11941201
throw new JSONException("JSONArray[" + index + "] not found.");
11951202
}
@@ -1513,15 +1520,15 @@ public Writer write(Writer writer, int indentFactor, int indent)
15131520
*
15141521
* @return a java.util.List containing the elements of this array
15151522
*/
1516-
public List<Object> toList() {
1517-
List<Object> results = new ArrayList<Object>(this.myArrayList.size());
1518-
for (Object element : this.myArrayList) {
1523+
public List<Serializable> toList() {
1524+
List<Serializable> results = new ArrayList<Serializable>(this.myArrayList.size());
1525+
for (Serializable element : this.myArrayList) {
15191526
if (element == null || JSONObject.NULL.equals(element)) {
15201527
results.add(null);
15211528
} else if (element instanceof JSONArray) {
1522-
results.add(((JSONArray) element).toList());
1529+
results.add((Serializable)((JSONArray) element).toList());
15231530
} else if (element instanceof JSONObject) {
1524-
results.add(((JSONObject) element).toMap());
1531+
results.add((Serializable)((JSONObject) element).toMap());
15251532
} else {
15261533
results.add(element);
15271534
}

JSONML.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ of this software and associated documentation files (the "Software"), to deal
2424
SOFTWARE.
2525
*/
2626

27+
import java.io.Serializable;
28+
2729
/**
2830
* This provides static methods to convert an XML text into a JSONArray or
2931
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
@@ -222,9 +224,13 @@ private static Object parse(
222224
}
223225
} else {
224226
if (ja != null) {
225-
ja.put(token instanceof String
226-
? keepStrings ? XML.unescape((String)token) :XML.stringToValue((String)token)
227-
: token);
227+
if (token instanceof Serializable) {
228+
ja.put(token instanceof String
229+
? keepStrings ? XML.unescape((String)token) :XML.stringToValue((String)token)
230+
: (Serializable)token);
231+
} else {
232+
throw x.syntaxError("Unserializable object");
233+
}
228234
}
229235
}
230236
}

0 commit comments

Comments
 (0)