Skip to content

Commit 039f794

Browse files
committed
JSONObject extends HashMap and JSONArray extends ArrayList passed all tests
need to add two test cases for just discovered json array and json object
1 parent 12b2e69 commit 039f794

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

.classpath

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,5 @@
44
<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
66
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
7-
<classpathentry kind="lib" path="F:/temp/maven-3.0/mvnrepository/com/google/code/gson/gson/2.1/gson-2.1.jar"/>
8-
<classpathentry kind="lib" path="F:/temp/maven-3.0/mvnrepository/commons-beanutils/commons-beanutils/1.8.0/commons-beanutils-1.8.0.jar"/>
9-
<classpathentry kind="lib" path="F:/temp/maven-3.0/mvnrepository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar"/>
10-
<classpathentry kind="lib" path="F:/temp/maven-3.0/mvnrepository/commons-lang/commons-lang/2.5/commons-lang-2.5.jar"/>
11-
<classpathentry kind="lib" path="F:/temp/maven-3.0/mvnrepository/net/sf/json-lib/json-lib/2.4/json-lib-2.4-jdk15.jar"/>
12-
<classpathentry kind="lib" path="F:/temp/maven-3.0/mvnrepository/net/sf/ezmorph/ezmorph/1.0.6/ezmorph-1.0.6.jar"/>
13-
<classpathentry kind="lib" path="F:/temp/maven-3.0/mvnrepository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
147
<classpathentry kind="output" path="target/classes"/>
158
</classpath>

src/main/java/MyTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55

66
import org.json.JSONArray;
7+
import org.json.JSONML;
78
import org.json.JSONObject;
89

910

@@ -40,6 +41,15 @@ public static void main(String[] args) {
4041

4142
System.out.println(jsonobject.toString());
4243

44+
String string = "<recipe name=\"bread\" prep_time=\"5 mins\" cook_time=\"3 hours\"> <title>Basic bread</title> <ingredient amount=\"8\" unit=\"dL\">Flour</ingredient> <ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient> <ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient> <ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient> <instructions> <step>Mix all ingredients together.</step> <step>Knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Knead again.</step> <step>Place in a bread baking tin.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Bake in the oven at 180(degrees)C for 30 minutes.</step> </instructions> </recipe> ";
45+
46+
jsonobject = JSONML.toJSONObject(string);
47+
48+
System.out.println(jsonobject.toString());
49+
System.out.println( JSONML.toString(jsonobject));
50+
51+
jsonarray = JSONML.toJSONArray(string);
52+
System.out.println(jsonarray);
4353
}
4454

4555
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ of this software and associated documentation files (the "Software"), to deal
3030
import java.lang.reflect.Array;
3131
import java.util.ArrayList;
3232
import java.util.Collection;
33-
import java.util.Iterator;
3433
import java.util.Map;
3534

3635
/**
@@ -586,6 +585,18 @@ public JSONArray put(Collection value) {
586585
super.add(new JSONArray(value));
587586
return this;
588587
}
588+
589+
/**
590+
* Put a value in the JSONArray, where the value will be a
591+
* JSONArray which is produced from a Collection.
592+
* Do not wrap JSONArray, pass by value
593+
* @param value A Collection value.
594+
* @return this.
595+
*/
596+
public JSONArray put(JSONArray value) {
597+
super.add(value);
598+
return this;
599+
}
589600

590601

591602
/**
@@ -638,6 +649,20 @@ public JSONArray put(Map value) {
638649
super.add(new JSONObject(value));
639650
return this;
640651
}
652+
653+
/**
654+
* samarjit for jsonobject same ref rather than a copy, in earlier
655+
* version this jsonarray.put usually went to jsonarray.put(Object)
656+
* Put a value in the JSONArray, where the value will be a
657+
* JSONObject which is produced from a Map.
658+
* @param value A Map value.
659+
* @return this.
660+
*/
661+
public JSONArray put(JSONObject value) {
662+
if(value == null )value = new JSONObject();
663+
super.add(value);
664+
return this;
665+
}
641666

642667

643668
/**

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,32 @@ public JSONObject(JSONTokener x) throws JSONException {
240240
* @throws JSONException
241241
*/
242242
public JSONObject(Map map) {
243-
super(map == null? new JSONObject():map );
243+
// super(map == null? new JSONObject():map ); //dont it needs to be wrapped
244244
//this.map = new HashMap();
245-
// if (map != null) {
246-
// Iterator i = map.entrySet().iterator();
247-
// while (i.hasNext()) {
248-
// Map.Entry e = (Map.Entry)i.next();
249-
// Object value = e.getValue();
250-
// if (value != null) {
251-
// super.put(e.getKey(), wrap(value));
252-
// }
253-
// }
254-
// }
245+
if (map != null) {
246+
Iterator i = map.entrySet().iterator();
247+
while (i.hasNext()) {
248+
Map.Entry e = (Map.Entry)i.next();
249+
Object value = e.getValue();
250+
if (value != null) {
251+
super.put(e.getKey(), wrap(value));
252+
}
253+
}
254+
}
255+
}
256+
257+
/**
258+
* Put a key/value pair in the JSONObject, where the value will be a
259+
* JSONObject which is produced from a Map.
260+
* For put(k,JSONObject value) would be call by ref. unlike wrapped Map as above.
261+
* @param key A key string.
262+
* @param value A Map value.
263+
* @return this.
264+
* @throws JSONException
265+
*/
266+
public JSONObject put(String key, JSONObject value) throws JSONException {
267+
super.put(key,value);
268+
return this;
255269
}
256270

257271

0 commit comments

Comments
 (0)