A JSON array. JSONObject supports java.util.List interface. @author FangYidong
| 17 | * @author FangYidong<fangyidong@yahoo.com.cn> |
| 18 | */ |
| 19 | public class JSONArray extends ArrayList<Object> implements List<Object>, JSONAware, JSONStreamAware { |
| 20 | private static final long serialVersionUID = 3957988303675231981L; |
| 21 | |
| 22 | /** |
| 23 | * Encode a list into JSON text and write it to out. |
| 24 | * If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level. |
| 25 | * |
| 26 | * @see org.json.simpleForBukkit.JSONValue#writeJSONString(Object, Writer) |
| 27 | * |
| 28 | * @param list |
| 29 | * @param out |
| 30 | */ |
| 31 | public static void writeJSONString(List<Object> list, Writer out) throws IOException{ |
| 32 | if(list == null){ |
| 33 | out.write("null"); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | boolean first = true; |
| 38 | Iterator<Object> iter=list.iterator(); |
| 39 | |
| 40 | out.write('['); |
| 41 | while(iter.hasNext()){ |
| 42 | if(first) |
| 43 | first = false; |
| 44 | else |
| 45 | out.write(','); |
| 46 | |
| 47 | Object value=iter.next(); |
| 48 | if(value == null){ |
| 49 | out.write("null"); |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | JSONValue.writeJSONString(value, out); |
| 54 | } |
| 55 | out.write(']'); |
| 56 | } |
| 57 | |
| 58 | public void writeJSONString(Writer out) throws IOException{ |
| 59 | writeJSONString(this, out); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Convert a list to JSON text. The result is a JSON array. |
| 64 | * If this list is also a JSONAware, JSONAware specific behaviours will be omitted at this top level. |
| 65 | * |
| 66 | * @see org.json.simpleForBukkit.JSONValue#toJSONString(Object) |
| 67 | * |
| 68 | * @param list |
| 69 | * @return JSON text, or "null" if list is null. |
| 70 | */ |
| 71 | public static String toJSONString(List<Object> list){ |
| 72 | if(list == null) |
| 73 | return "null"; |
| 74 | |
| 75 | boolean first = true; |
| 76 | StringBuffer sb = new StringBuffer(); |
nothing calls this directly
no outgoing calls
no test coverage detected