Skip to content

Commit 84a74e6

Browse files
committed
JSONArray implements List and JSONObject implements Map
1 parent 926c5ec commit 84a74e6

File tree

7 files changed

+457
-218
lines changed

7 files changed

+457
-218
lines changed

.classpath

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
4-
<attributes>
5-
<attribute name="optional" value="true"/>
6-
<attribute name="maven.pomderived" value="true"/>
7-
</attributes>
8-
</classpathentry>
9-
<classpathentry kind="src" output="target/classes" path="src/main/java">
10-
<attributes>
11-
<attribute name="optional" value="true"/>
12-
<attribute name="maven.pomderived" value="true"/>
13-
</attributes>
14-
</classpathentry>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
4+
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
6+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
157
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
16-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
17-
<attributes>
18-
<attribute name="maven.pomderived" value="true"/>
19-
</attributes>
20-
</classpathentry>
21-
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22-
<attributes>
23-
<attribute name="maven.pomderived" value="true"/>
24-
</attributes>
25-
</classpathentry>
268
<classpathentry kind="output" path="target/classes"/>
279
</classpath>

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
<version>4.4</version>
2929
<scope>test</scope>
3030
</dependency>
31+
<dependency>
32+
<groupId>org.freemarker</groupId>
33+
<artifactId>freemarker</artifactId>
34+
<version>2.3.19</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>ognl</groupId>
39+
<artifactId>ognl</artifactId>
40+
<version>3.0.4</version>
41+
<scope>test</scope>
42+
</dependency>
3143
</dependencies>
3244
<build>
3345
<defaultGoal>install</defaultGoal>

src/bak/JSONArray.java

Lines changed: 39 additions & 12 deletions
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
/**
@@ -151,14 +150,15 @@ public JSONArray(String source) throws JSONException {
151150
* @param collection A Collection.
152151
*/
153152
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-
}
153+
super((collection == null)?new ArrayList():collection);
154+
// if(collection == null)collection = new ArrayList();
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+
// }
162162
}
163163

164164

@@ -585,6 +585,18 @@ public JSONArray put(Collection value) {
585585
super.add(new JSONArray(value));
586586
return this;
587587
}
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 ref
593+
* @param value A Collection value.
594+
* @return this.
595+
*/
596+
public JSONArray put(JSONArray value) {
597+
super.add(value);
598+
return this;
599+
}
588600

589601

590602
/**
@@ -633,15 +645,30 @@ public JSONArray put(long value) {
633645
* @return this.
634646
*/
635647
public JSONArray put(Map value) {
648+
if(value == null )value = new JSONObject();
636649
super.add(new JSONObject(value));
637650
return this;
638651
}
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+
}
639666

640667

641668
/**
642669
* Append an object value. This increases the array's length by one.
643670
* @param value An object value. The value should be a
644-
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
671+
* Boolean, Double, Integer, JSONArray,<strike> JSONObject,</strike> Long, or String, or the
645672
* JSONObject.NULL object.
646673
* @return this.
647674
*/
@@ -682,7 +709,7 @@ public JSONArray put(int index, Collection value) throws JSONException {
682709
throw new JSONException("JSONArray[" + index + "] not found.");
683710
}
684711
if (index < this.length()) {
685-
super.set(index, value);
712+
super.set(index, new JSONArray(value));
686713
} else {
687714
while (index != this.length()) {
688715
this.put(JSONObject.NULL);
@@ -754,7 +781,7 @@ public JSONArray put(int index, Map value) throws JSONException {
754781
throw new JSONException("JSONArray[" + index + "] not found.");
755782
}
756783
if (index < this.length()) {
757-
super.set(index, value);
784+
super.set(index, new JSONObject(value));
758785
} else {
759786
while (index != this.length()) {
760787
this.put(JSONObject.NULL);

src/bak/JSONObject.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ of this software and associated documentation files (the "Software"), to deal
3232
import java.lang.reflect.Modifier;
3333
import java.util.Collection;
3434
import java.util.Enumeration;
35-
import java.util.HashMap;
3635
import java.util.Iterator;
36+
import java.util.LinkedHashMap;
3737
import java.util.Locale;
3838
import java.util.Map;
3939
import java.util.ResourceBundle;
@@ -93,7 +93,7 @@ of this software and associated documentation files (the "Software"), to deal
9393
* @author JSON.org
9494
* @version 2012-10-27
9595
*/
96-
public class JSONObject extends HashMap{
96+
public class JSONObject extends LinkedHashMap{
9797

9898
/**
9999
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
@@ -240,7 +240,7 @@ public JSONObject(JSONTokener x) throws JSONException {
240240
* @throws JSONException
241241
*/
242242
public JSONObject(Map map) {
243-
super();
243+
// super(map == null? new JSONObject():map ); //dont it needs to be wrapped
244244
//this.map = new HashMap();
245245
if (map != null) {
246246
Iterator i = map.entrySet().iterator();
@@ -254,6 +254,20 @@ public JSONObject(Map map) {
254254
}
255255
}
256256

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;
269+
}
270+
257271

258272
/**
259273
* Construct a JSONObject from an Object using bean getters.
@@ -353,6 +367,8 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
353367
target = nextTarget;
354368
}
355369
target.put(path[last], bundle.getString((String)key));
370+
// System.out.println("REMOVE:"+(String)key+" "+bundle.getString((String)key)+" "+this);
371+
356372
}
357373
}
358374
}
@@ -696,7 +712,7 @@ public JSONObject increment(String key) throws JSONException {
696712
* the value is the JSONObject.NULL object.
697713
*/
698714
public boolean isNull(String key) {
699-
System.out.println("REMOVE:"+this.opt(key));
715+
// System.out.println("REMOVE:"+this.opt(key));
700716
return JSONObject.NULL.equals(this.opt(key));
701717
}
702718

@@ -1098,7 +1114,8 @@ public JSONObject put(String key, long value) throws JSONException {
10981114
* @throws JSONException
10991115
*/
11001116
public JSONObject put(String key, Map value) throws JSONException {
1101-
super.put(key, new JSONObject(value));
1117+
if(value == null )value= new JSONObject();
1118+
super.put(key, value);
11021119
return this;
11031120
}
11041121

@@ -1108,7 +1125,7 @@ public JSONObject put(String key, Map value) throws JSONException {
11081125
* then the key will be removed from the JSONObject if it is present.
11091126
* @param key A key string.
11101127
* @param value An object which is the value. It should be of one of these
1111-
* types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
1128+
* types: Boolean, Double, Integer, JSONArray, <strike>JSONObject,</strike> Long, String,
11121129
* or the JSONObject.NULL object.
11131130
* @return this.
11141131
* @throws JSONException If the value is non-finite number

0 commit comments

Comments
 (0)