Skip to content

Commit 12b2e69

Browse files
committed
jsonobject and json array tests fixed. tofix jsonm tests
1 parent 3513950 commit 12b2e69

File tree

7 files changed

+96
-31
lines changed

7 files changed

+96
-31
lines changed

.classpath

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4-
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
5-
<classpathentry kind="output" path="target/classes"/>
6-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
7-
<classpathentry kind="var" path="M2_REPO/junit/junit/4.4/junit-4.4.jar"/>
8-
</classpath>
3+
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
4+
<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<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"/>
14+
<classpathentry kind="output" path="target/classes"/>
15+
</classpath>

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@
2828
<version>4.4</version>
2929
<scope>test</scope>
3030
</dependency>
31-
</dependencies>
31+
<dependency>
32+
<groupId>net.sf.json-lib</groupId>
33+
<artifactId>json-lib</artifactId>
34+
<version>2.4</version>
35+
<classifier>jdk15</classifier>
36+
</dependency>
37+
</dependencies>
3238
<build>
3339
<defaultGoal>install</defaultGoal>
3440
<plugins>

src/main/java/MyTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.ArrayList;
2+
import java.util.Collection;
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
10+
11+
12+
13+
14+
public class MyTest {
15+
16+
/**
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
//{"stooge":"Shemp","map":null,"stooges":["Curly","Larry","Moe"],"collection":[],
21+
//"stoogearray":[["Curly","Larry","Moe"]],"array":[null,null]}
22+
//{"stooge":"Shemp","stooges":["Curly","Larry","Moe"],"stoogearray":["Curly","Larry","Moe"],"array":[]}
23+
24+
Collection<Object> collection = new ArrayList<Object>();
25+
Map<String, Object> map = new HashMap<String, Object>();
26+
27+
JSONObject jsonobject = new JSONObject(map);
28+
JSONArray jsonarray = new JSONArray(collection);
29+
jsonobject.put("stooge", "Joe DeRita");
30+
jsonobject.put("stooge", "Shemp");
31+
jsonobject.accumulate("stooges", "Curly");
32+
jsonobject.accumulate("stooges", "Larry");
33+
jsonobject.accumulate("stooges", "Moe");
34+
jsonobject.accumulate("stoogearray", jsonobject.get("stooges"));
35+
jsonobject.put("map", map);
36+
jsonobject.put("collection", collection);
37+
jsonobject.put("array", jsonarray);
38+
jsonarray.add(map);
39+
jsonarray.add(collection);
40+
41+
System.out.println(jsonobject.toString());
42+
43+
}
44+
45+
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,15 @@ public JSONArray(String source) throws JSONException {
151151
* @param collection A Collection.
152152
*/
153153
public JSONArray(Collection collection) {
154-
super();
155-
// this.myArrayList = new ArrayList();
156-
if (collection != null) {
157-
Iterator iter = collection.iterator();
158-
while (iter.hasNext()) {
159-
super.add(JSONObject.wrap(iter.next()));
160-
}
161-
}
154+
super((collection == null)?new ArrayList():collection);
155+
// if(collection == null)collection = new ArrayList();
156+
//// this.myArrayList = new ArrayList();
157+
// if (collection != null) {
158+
// Iterator iter = collection.iterator();
159+
// while (iter.hasNext()) {
160+
// super.add(JSONObject.wrap(iter.next()));
161+
// }
162+
// }
162163
}
163164

164165

@@ -633,6 +634,7 @@ public JSONArray put(long value) {
633634
* @return this.
634635
*/
635636
public JSONArray put(Map value) {
637+
if(value == null )value = new JSONObject();
636638
super.add(new JSONObject(value));
637639
return this;
638640
}
@@ -682,7 +684,7 @@ public JSONArray put(int index, Collection value) throws JSONException {
682684
throw new JSONException("JSONArray[" + index + "] not found.");
683685
}
684686
if (index < this.length()) {
685-
super.set(index, value);
687+
super.set(index, new JSONArray(value));
686688
} else {
687689
while (index != this.length()) {
688690
this.put(JSONObject.NULL);
@@ -754,7 +756,7 @@ public JSONArray put(int index, Map value) throws JSONException {
754756
throw new JSONException("JSONArray[" + index + "] not found.");
755757
}
756758
if (index < this.length()) {
757-
super.set(index, value);
759+
super.set(index, new JSONObject(value));
758760
} else {
759761
while (index != this.length()) {
760762
this.put(JSONObject.NULL);

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,18 @@ public JSONObject(JSONTokener x) throws JSONException {
240240
* @throws JSONException
241241
*/
242242
public JSONObject(Map map) {
243-
super();
243+
super(map == null? new JSONObject():map );
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+
// }
255255
}
256256

257257

@@ -353,6 +353,8 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
353353
target = nextTarget;
354354
}
355355
target.put(path[last], bundle.getString((String)key));
356+
// System.out.println("REMOVE:"+(String)key+" "+bundle.getString((String)key)+" "+this);
357+
356358
}
357359
}
358360
}
@@ -696,7 +698,7 @@ public JSONObject increment(String key) throws JSONException {
696698
* the value is the JSONObject.NULL object.
697699
*/
698700
public boolean isNull(String key) {
699-
System.out.println("REMOVE:"+this.opt(key));
701+
// System.out.println("REMOVE:"+this.opt(key));
700702
return JSONObject.NULL.equals(this.opt(key));
701703
}
702704

@@ -1098,7 +1100,8 @@ public JSONObject put(String key, long value) throws JSONException {
10981100
* @throws JSONException
10991101
*/
11001102
public JSONObject put(String key, Map value) throws JSONException {
1101-
super.put(key, new JSONObject(value));
1103+
if(value == null )value= new JSONObject();
1104+
super.put(key, value);
11021105
return this;
11031106
}
11041107

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,8 @@ public void testPut_MapAndSpecificIndex()
13801380
jsonarray = new JSONArray();
13811381
jsonarray.put(0, a);
13821382
jsonarray.put(1, b);
1383+
//changed from normal get to getJSONObject
1384+
//as .get() is intended to return the original underlying object
13831385
assertEquals(new JSONObject(a).toString(), jsonarray.get(0).toString());
13841386
assertEquals(new JSONObject(b).toString(), jsonarray.get(1).toString());
13851387
jsonarray.put(0, c);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public void testPut_CollectionAndMap()
590590
jsonobject.toString());
591591

592592
Collection<Object> collection = null;
593-
Map<String, Object> map = null;
593+
Map<String, Object> map = null;//new HashMap<String, Object>();
594594

595595
jsonobject = new JSONObject(map);
596596
jsonarray = new JSONArray(collection);

0 commit comments

Comments
 (0)