Skip to content

Commit db197da

Browse files
committed
Pass by ref tests added
1 parent 039f794 commit db197da

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

src/main/java/MyTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void main(String[] args) {
2222
//"stoogearray":[["Curly","Larry","Moe"]],"array":[null,null]}
2323
//{"stooge":"Shemp","stooges":["Curly","Larry","Moe"],"stoogearray":["Curly","Larry","Moe"],"array":[]}
2424

25-
Collection<Object> collection = new ArrayList<Object>();
25+
/* Collection<Object> collection = new ArrayList<Object>();
2626
Map<String, Object> map = new HashMap<String, Object>();
2727
2828
JSONObject jsonobject = new JSONObject(map);
@@ -49,7 +49,13 @@ public static void main(String[] args) {
4949
System.out.println( JSONML.toString(jsonobject));
5050
5151
jsonarray = JSONML.toJSONArray(string);
52-
System.out.println(jsonarray);
52+
System.out.println(jsonarray);*/
53+
JSONObject jsonobject = new JSONObject();
54+
JSONObject localobj = new JSONObject();
55+
localobj.put("a","old");
56+
jsonobject.put("passre",localobj);
57+
localobj.put("b","new");
58+
System.out.println(jsonobject.toString());
5359
}
5460

5561
}

src/main/java/org/json/JSONArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ public JSONArray put(Collection value) {
589589
/**
590590
* Put a value in the JSONArray, where the value will be a
591591
* JSONArray which is produced from a Collection.
592-
* Do not wrap JSONArray, pass by value
592+
* Do not wrap JSONArray, pass by ref
593593
* @param value A Collection value.
594594
* @return this.
595595
*/
@@ -668,7 +668,7 @@ public JSONArray put(JSONObject value) {
668668
/**
669669
* Append an object value. This increases the array's length by one.
670670
* @param value An object value. The value should be a
671-
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
671+
* Boolean, Double, Integer, JSONArray,<strike> JSONObject,</strike> Long, or String, or the
672672
* JSONObject.NULL object.
673673
* @return this.
674674
*/

src/main/java/org/json/JSONObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ public JSONObject put(String key, Map value) throws JSONException {
11251125
* then the key will be removed from the JSONObject if it is present.
11261126
* @param key A key string.
11271127
* @param value An object which is the value. It should be of one of these
1128-
* types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
1128+
* types: Boolean, Double, Integer, JSONArray, <strike>JSONObject,</strike> Long, String,
11291129
* or the JSONObject.NULL object.
11301130
* @return this.
11311131
* @throws JSONException If the value is non-finite number

src/test/java/org/json/tests/TestJSONArray.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,4 +1439,29 @@ public void testRemove()
14391439
}
14401440
}
14411441

1442+
/**
1443+
* This should call JSONArray.put(JSONArray value) hence pass by ref
1444+
*/
1445+
public void testPassByRefJSONArray(){
1446+
JSONArray localar = new JSONArray();
1447+
jsonarray = new JSONArray();
1448+
localar.put("old");
1449+
jsonarray.put(localar);
1450+
localar.put("new");
1451+
assertEquals("[[\"old\",\"new\"]]",jsonarray.toString());
1452+
}
1453+
1454+
/**
1455+
* This should call JSONArray.put(JSONObject value) instead of JSONObject.put(Map value)
1456+
* to simulate pass by ref
1457+
*/
1458+
public void testPassByRefJSONObject(){
1459+
JSONObject jsonobject = new JSONObject();
1460+
jsonarray = new JSONArray();
1461+
jsonobject.put("a","old");
1462+
jsonarray.put(jsonobject);
1463+
jsonobject.put("b","new");
1464+
assertEquals("[{\"b\":\"new\",\"a\":\"old\"}]",jsonarray.toString());
1465+
}
1466+
14421467
}

src/test/java/org/json/tests/TestJSONObject.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,4 +2507,29 @@ public static void testDoubleToString()
25072507
assertEquals("1.0E89", JSONObject.doubleToString(10e88));
25082508
assertEquals("1.0E89", JSONObject.doubleToString(10E88));
25092509
}
2510+
2511+
/**
2512+
* This should call JSONObject.put(Object value) hence pass by ref
2513+
*/
2514+
public void testPassByRefJSONArray(){
2515+
jsonobject = new JSONObject();
2516+
jsonarray = new JSONArray();
2517+
jsonarray.put("old");
2518+
jsonobject.put("ar",jsonarray);
2519+
jsonarray.put("new");
2520+
assertEquals("{\"ar\":[\"old\",\"new\"]}",jsonobject.toString());
2521+
}
2522+
2523+
/**
2524+
* This should call JSONObject.put(JSONObject value) instead of JSONObject.put(Map value)
2525+
* to simulate pass by ref
2526+
*/
2527+
public void testPassByRefJSONObject(){
2528+
jsonobject = new JSONObject();
2529+
JSONObject localobj = new JSONObject();
2530+
localobj.put("a","old");
2531+
jsonobject.put("passrefmap",localobj);
2532+
localobj.put("b","new");
2533+
assertEquals("{\"passrefmap\":{\"b\":\"new\",\"a\":\"old\"}}",jsonobject.toString());
2534+
}
25102535
}

0 commit comments

Comments
 (0)