michael@0: /* michael@0: * $Id: JSONArray.java,v 1.1 2006/04/15 14:10:48 platform Exp $ michael@0: * Created on 2006-4-10 michael@0: */ michael@0: package org.json.simple; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.Writer; michael@0: import java.util.ArrayList; michael@0: import java.util.Iterator; michael@0: import java.util.List; michael@0: michael@0: michael@0: /** michael@0: * A JSON array. JSONObject supports java.util.List interface. michael@0: * michael@0: * @author FangYidong michael@0: */ michael@0: public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamAware { michael@0: private static final long serialVersionUID = 3957988303675231981L; michael@0: michael@0: /** michael@0: * Encode a list into JSON text and write it to out. michael@0: * If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level. michael@0: * michael@0: * @see org.json.simple.JSONValue#writeJSONString(Object, Writer) michael@0: * michael@0: * @param list michael@0: * @param out michael@0: */ michael@0: public static void writeJSONString(List list, Writer out) throws IOException{ michael@0: if(list == null){ michael@0: out.write("null"); michael@0: return; michael@0: } michael@0: michael@0: boolean first = true; michael@0: Iterator iter=list.iterator(); michael@0: michael@0: out.write('['); michael@0: while(iter.hasNext()){ michael@0: if(first) michael@0: first = false; michael@0: else michael@0: out.write(','); michael@0: michael@0: Object value=iter.next(); michael@0: if(value == null){ michael@0: out.write("null"); michael@0: continue; michael@0: } michael@0: michael@0: JSONValue.writeJSONString(value, out); michael@0: } michael@0: out.write(']'); michael@0: } michael@0: michael@0: public void writeJSONString(Writer out) throws IOException{ michael@0: writeJSONString(this, out); michael@0: } michael@0: michael@0: /** michael@0: * Convert a list to JSON text. The result is a JSON array. michael@0: * If this list is also a JSONAware, JSONAware specific behaviours will be omitted at this top level. michael@0: * michael@0: * @see org.json.simple.JSONValue#toJSONString(Object) michael@0: * michael@0: * @param list michael@0: * @return JSON text, or "null" if list is null. michael@0: */ michael@0: public static String toJSONString(List list){ michael@0: if(list == null) michael@0: return "null"; michael@0: michael@0: boolean first = true; michael@0: StringBuffer sb = new StringBuffer(); michael@0: Iterator iter=list.iterator(); michael@0: michael@0: sb.append('['); michael@0: while(iter.hasNext()){ michael@0: if(first) michael@0: first = false; michael@0: else michael@0: sb.append(','); michael@0: michael@0: Object value=iter.next(); michael@0: if(value == null){ michael@0: sb.append("null"); michael@0: continue; michael@0: } michael@0: sb.append(JSONValue.toJSONString(value)); michael@0: } michael@0: sb.append(']'); michael@0: return sb.toString(); michael@0: } michael@0: michael@0: public String toJSONString(){ michael@0: return toJSONString(this); michael@0: } michael@0: michael@0: public String toString() { michael@0: return toJSONString(); michael@0: } michael@0: michael@0: michael@0: michael@0: }