@@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
2525 */
2626
2727import java .io .IOException ;
28+ import java .io .Serializable ;
2829import java .io .StringWriter ;
2930import java .io .Writer ;
3031import 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 }
0 commit comments