mirror of
https://github.com/fangyidong/json-simple.git
synced 2025-12-06 15:30: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.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* A JSON array. JSONObject supports java.util.List interface.
|
||||
*
|
||||
* @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;
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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)
|
||||
*
|
||||
* @param list
|
||||
* @param collection
|
||||
* @param out
|
||||
*/
|
||||
public static void writeJSONString(List list, Writer out) throws IOException{
|
||||
if(list == null){
|
||||
public static void writeJSONString(Collection collection, Writer out) throws IOException{
|
||||
if(collection == null){
|
||||
out.write("null");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean first = true;
|
||||
Iterator iter=list.iterator();
|
||||
Iterator iter=collection.iterator();
|
||||
|
||||
out.write('[');
|
||||
while(iter.hasNext()){
|
||||
@@ -65,16 +81,16 @@ public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamA
|
||||
*
|
||||
* @see org.json.simple.JSONValue#toJSONString(Object)
|
||||
*
|
||||
* @param list
|
||||
* @param collection
|
||||
* @return JSON text, or "null" if list is null.
|
||||
*/
|
||||
public static String toJSONString(List list){
|
||||
if(list == null)
|
||||
public static String toJSONString(Collection collection){
|
||||
if(collection == null)
|
||||
return "null";
|
||||
|
||||
boolean first = true;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
Iterator iter=list.iterator();
|
||||
Iterator iter=collection.iterator();
|
||||
|
||||
sb.append('[');
|
||||
while(iter.hasNext()){
|
||||
@@ -101,7 +117,4 @@ public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamA
|
||||
public String toString() {
|
||||
return toJSONString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
// import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.simple.parser.JSONParser;
|
||||
@@ -146,8 +147,8 @@ public class JSONValue {
|
||||
return;
|
||||
}
|
||||
|
||||
if(value instanceof List){
|
||||
JSONArray.writeJSONString((List)value, out);
|
||||
if(value instanceof Collection){
|
||||
JSONArray.writeJSONString((Collection)value, out);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -201,8 +202,8 @@ public class JSONValue {
|
||||
if(value instanceof Map)
|
||||
return JSONObject.toJSONString((Map)value);
|
||||
|
||||
if(value instanceof List)
|
||||
return JSONArray.toJSONString((List)value);
|
||||
if(value instanceof Collection)
|
||||
return JSONArray.toJSONString((Collection)value);
|
||||
|
||||
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