@@ -79,7 +79,7 @@ of this software and associated documentation files (the "Software"), to deal
7979 * @author JSON.org
8080 * @version 2012-04-20
8181 */
82- public class JSONArray implements Serializable {
82+ public class JSONArray extends JSONType implements Serializable {
8383
8484 /**
8585 * Generated on January 20th, 2013.
@@ -91,6 +91,22 @@ public class JSONArray implements Serializable {
9191 */
9292 private final ArrayList <Object > myArrayList ;
9393
94+ private void setParent (Object object , JSONArray parent ) {
95+ if ( object != null && object instanceof JSONType ) {
96+ ((JSONType ) object ).setParent (parent );
97+ }
98+ }
99+
100+ private void store (Object object ) {
101+ setParent (object , this );
102+ this .myArrayList .add (object );
103+ }
104+
105+ private void store (int index , Object object ) {
106+ setParent (object , this );
107+ this .myArrayList .set (index , object );
108+ }
109+
94110
95111 /**
96112 * Construct an empty JSONArray.
@@ -114,10 +130,10 @@ public JSONArray(JSONTokener x) throws JSONException {
114130 for (;;) {
115131 if (x .nextClean () == ',' ) {
116132 x .back ();
117- this .myArrayList . add (JSONObject .NULL );
133+ this .store (JSONObject .NULL );
118134 } else {
119135 x .back ();
120- this .myArrayList . add (x .nextValue ());
136+ this .store (x .nextValue ());
121137 }
122138 switch (x .nextClean ()) {
123139 case ';' :
@@ -157,7 +173,7 @@ public JSONArray(Collection<?> collection) {
157173 this .myArrayList = new ArrayList <Object >();
158174 if (collection != null ) {
159175 for ( Object object : collection ) {
160- this .myArrayList . add (JSONObject .wrap (object ));
176+ this .store (JSONObject .wrap (object ));
161177 }
162178 }
163179 }
@@ -644,7 +660,7 @@ public JSONArray put(Map<String, Object> value) {
644660 * @return this.
645661 */
646662 public JSONArray put (Object value ) {
647- this .myArrayList . add (value );
663+ this .store (value );
648664 return this ;
649665 }
650666
@@ -758,7 +774,7 @@ public JSONArray put(int index, Object value) throws JSONException {
758774 throw new JSONException ("JSONArray[" + index + "] not found." );
759775 }
760776 if (index < this .length ()) {
761- this .myArrayList . set (index , value );
777+ this .store (index , value );
762778 } else {
763779 while (index != this .length ()) {
764780 this .put (JSONObject .NULL );
@@ -776,9 +792,10 @@ public JSONArray put(int index, Object value) throws JSONException {
776792 * or null if there was no value.
777793 */
778794 public Object remove (int index ) {
779- Object o = this .opt (index );
795+ final Object object = this .opt (index );
796+ setParent (object , null );
780797 this .myArrayList .remove (index );
781- return o ;
798+ return object ;
782799 }
783800
784801
@@ -795,11 +812,11 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
795812 if (names == null || names .length () == 0 || this .length () == 0 ) {
796813 return null ;
797814 }
798- JSONObject jo = new JSONObject ();
815+ JSONObject jsonObject = new JSONObject ();
799816 for (int i = 0 ; i < names .length (); i += 1 ) {
800- jo .put (names .getString (i ), this .opt (i ));
817+ jsonObject .put (names .getString (i ), this .opt (i ));
801818 }
802- return jo ;
819+ return jsonObject ;
803820 }
804821
805822
0 commit comments