mirror of
https://github.com/fangyidong/json-simple.git
synced 2025-12-07 07:50:54 +03:00
Made JSONArrays work with Collections instead of just Lists. Added a constructor for JSONArrays that takes an initial collection. Added supporting unit tests.
This commit is contained in:
@@ -7,35 +7,51 @@ package org.json.simple;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A JSON array. JSONObject supports java.util.List interface.
|
* A JSON array. JSONObject supports java.util.List interface.
|
||||||
*
|
*
|
||||||
* @author FangYidong<fangyidong@yahoo.com.cn>
|
* @author FangYidong<fangyidong@yahoo.com.cn>
|
||||||
*/
|
*/
|
||||||
public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamAware {
|
public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
|
||||||
private static final long serialVersionUID = 3957988303675231981L;
|
private static final long serialVersionUID = 3957988303675231981L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an empty JSONArray.
|
||||||
|
*/
|
||||||
|
public JSONArray(){
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a JSONArray containing the elements of the specified
|
||||||
|
* collection, in the order they are returned by the collection's iterator.
|
||||||
|
*
|
||||||
|
* @param c the collection whose elements are to be placed into this JSONArray
|
||||||
|
*/
|
||||||
|
public JSONArray(Collection c){
|
||||||
|
super(c);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode a list into JSON text and write it to out.
|
* Encode a list into JSON text and write it to out.
|
||||||
* If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level.
|
* If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level.
|
||||||
*
|
*
|
||||||
* @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
|
* @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
|
||||||
*
|
*
|
||||||
* @param list
|
* @param collection
|
||||||
* @param out
|
* @param out
|
||||||
*/
|
*/
|
||||||
public static void writeJSONString(List list, Writer out) throws IOException{
|
public static void writeJSONString(Collection collection, Writer out) throws IOException{
|
||||||
if(list == null){
|
if(collection == null){
|
||||||
out.write("null");
|
out.write("null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
Iterator iter=list.iterator();
|
Iterator iter=collection.iterator();
|
||||||
|
|
||||||
out.write('[');
|
out.write('[');
|
||||||
while(iter.hasNext()){
|
while(iter.hasNext()){
|
||||||
@@ -65,16 +81,16 @@ public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamA
|
|||||||
*
|
*
|
||||||
* @see org.json.simple.JSONValue#toJSONString(Object)
|
* @see org.json.simple.JSONValue#toJSONString(Object)
|
||||||
*
|
*
|
||||||
* @param list
|
* @param collection
|
||||||
* @return JSON text, or "null" if list is null.
|
* @return JSON text, or "null" if list is null.
|
||||||
*/
|
*/
|
||||||
public static String toJSONString(List list){
|
public static String toJSONString(Collection collection){
|
||||||
if(list == null)
|
if(collection == null)
|
||||||
return "null";
|
return "null";
|
||||||
|
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
Iterator iter=list.iterator();
|
Iterator iter=collection.iterator();
|
||||||
|
|
||||||
sb.append('[');
|
sb.append('[');
|
||||||
while(iter.hasNext()){
|
while(iter.hasNext()){
|
||||||
@@ -101,7 +117,4 @@ public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamA
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return toJSONString();
|
return toJSONString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ import java.io.IOException;
|
|||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.List;
|
import java.util.Collection;
|
||||||
|
// import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
@@ -146,8 +147,8 @@ public class JSONValue {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(value instanceof List){
|
if(value instanceof Collection){
|
||||||
JSONArray.writeJSONString((List)value, out);
|
JSONArray.writeJSONString((Collection)value, out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,8 +202,8 @@ public class JSONValue {
|
|||||||
if(value instanceof Map)
|
if(value instanceof Map)
|
||||||
return JSONObject.toJSONString((Map)value);
|
return JSONObject.toJSONString((Map)value);
|
||||||
|
|
||||||
if(value instanceof List)
|
if(value instanceof Collection)
|
||||||
return JSONArray.toJSONString((List)value);
|
return JSONArray.toJSONString((Collection)value);
|
||||||
|
|
||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
64
src/test/java/org/json/simple/JSONArrayTest.java
Normal file
64
src/test/java/org/json/simple/JSONArrayTest.java
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package org.json.simple;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
import org.json.simple.parser.JSONParser;
|
||||||
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
public class JSONArrayTest extends TestCase {
|
||||||
|
|
||||||
|
public void testJSONArray() {
|
||||||
|
final JSONArray jsonArray = new JSONArray();
|
||||||
|
|
||||||
|
assertEquals("[]", jsonArray.toJSONString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSONArrayCollection() {
|
||||||
|
final ArrayList testList = new ArrayList();
|
||||||
|
testList.add("First item");
|
||||||
|
testList.add("Second item");
|
||||||
|
|
||||||
|
final JSONArray jsonArray = new JSONArray(testList);
|
||||||
|
|
||||||
|
assertEquals("[\"First item\",\"Second item\"]", jsonArray.toJSONString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testWriteJSONStringCollectionWriter() throws IOException, ParseException {
|
||||||
|
final HashSet testSet = new HashSet();
|
||||||
|
testSet.add("First item");
|
||||||
|
testSet.add("Second item");
|
||||||
|
|
||||||
|
final JSONArray jsonArray = new JSONArray(testSet);
|
||||||
|
final StringWriter writer = new StringWriter();
|
||||||
|
|
||||||
|
jsonArray.writeJSONString(writer);
|
||||||
|
|
||||||
|
final JSONParser parser = new JSONParser();
|
||||||
|
final JSONArray parsedArray = (JSONArray)parser.parse(writer.toString());
|
||||||
|
|
||||||
|
assertTrue(parsedArray.containsAll(jsonArray));
|
||||||
|
assertTrue(jsonArray.containsAll(parsedArray));
|
||||||
|
assertEquals(2, jsonArray.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testToJSONStringCollection() throws ParseException {
|
||||||
|
final HashSet testSet = new HashSet();
|
||||||
|
testSet.add("First item");
|
||||||
|
testSet.add("Second item");
|
||||||
|
|
||||||
|
final JSONArray jsonArray = new JSONArray(testSet);
|
||||||
|
|
||||||
|
final JSONParser parser = new JSONParser();
|
||||||
|
final JSONArray parsedArray = (JSONArray)parser.parse(jsonArray.toJSONString());
|
||||||
|
|
||||||
|
assertTrue(parsedArray.containsAll(jsonArray));
|
||||||
|
assertTrue(jsonArray.containsAll(parsedArray));
|
||||||
|
assertEquals(2, jsonArray.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user