Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * $Id: JSONArray.java,v 1.1 2006/04/15 14:10:48 platform Exp $ |
michael@0 | 3 | * Created on 2006-4-10 |
michael@0 | 4 | */ |
michael@0 | 5 | package org.json.simple; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.IOException; |
michael@0 | 8 | import java.io.Writer; |
michael@0 | 9 | import java.util.ArrayList; |
michael@0 | 10 | import java.util.Iterator; |
michael@0 | 11 | import java.util.List; |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * A JSON array. JSONObject supports java.util.List interface. |
michael@0 | 16 | * |
michael@0 | 17 | * @author FangYidong<fangyidong@yahoo.com.cn> |
michael@0 | 18 | */ |
michael@0 | 19 | public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamAware { |
michael@0 | 20 | private static final long serialVersionUID = 3957988303675231981L; |
michael@0 | 21 | |
michael@0 | 22 | /** |
michael@0 | 23 | * Encode a list into JSON text and write it to out. |
michael@0 | 24 | * If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level. |
michael@0 | 25 | * |
michael@0 | 26 | * @see org.json.simple.JSONValue#writeJSONString(Object, Writer) |
michael@0 | 27 | * |
michael@0 | 28 | * @param list |
michael@0 | 29 | * @param out |
michael@0 | 30 | */ |
michael@0 | 31 | public static void writeJSONString(List list, Writer out) throws IOException{ |
michael@0 | 32 | if(list == null){ |
michael@0 | 33 | out.write("null"); |
michael@0 | 34 | return; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | boolean first = true; |
michael@0 | 38 | Iterator iter=list.iterator(); |
michael@0 | 39 | |
michael@0 | 40 | out.write('['); |
michael@0 | 41 | while(iter.hasNext()){ |
michael@0 | 42 | if(first) |
michael@0 | 43 | first = false; |
michael@0 | 44 | else |
michael@0 | 45 | out.write(','); |
michael@0 | 46 | |
michael@0 | 47 | Object value=iter.next(); |
michael@0 | 48 | if(value == null){ |
michael@0 | 49 | out.write("null"); |
michael@0 | 50 | continue; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | JSONValue.writeJSONString(value, out); |
michael@0 | 54 | } |
michael@0 | 55 | out.write(']'); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | public void writeJSONString(Writer out) throws IOException{ |
michael@0 | 59 | writeJSONString(this, out); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Convert a list to JSON text. The result is a JSON array. |
michael@0 | 64 | * If this list is also a JSONAware, JSONAware specific behaviours will be omitted at this top level. |
michael@0 | 65 | * |
michael@0 | 66 | * @see org.json.simple.JSONValue#toJSONString(Object) |
michael@0 | 67 | * |
michael@0 | 68 | * @param list |
michael@0 | 69 | * @return JSON text, or "null" if list is null. |
michael@0 | 70 | */ |
michael@0 | 71 | public static String toJSONString(List list){ |
michael@0 | 72 | if(list == null) |
michael@0 | 73 | return "null"; |
michael@0 | 74 | |
michael@0 | 75 | boolean first = true; |
michael@0 | 76 | StringBuffer sb = new StringBuffer(); |
michael@0 | 77 | Iterator iter=list.iterator(); |
michael@0 | 78 | |
michael@0 | 79 | sb.append('['); |
michael@0 | 80 | while(iter.hasNext()){ |
michael@0 | 81 | if(first) |
michael@0 | 82 | first = false; |
michael@0 | 83 | else |
michael@0 | 84 | sb.append(','); |
michael@0 | 85 | |
michael@0 | 86 | Object value=iter.next(); |
michael@0 | 87 | if(value == null){ |
michael@0 | 88 | sb.append("null"); |
michael@0 | 89 | continue; |
michael@0 | 90 | } |
michael@0 | 91 | sb.append(JSONValue.toJSONString(value)); |
michael@0 | 92 | } |
michael@0 | 93 | sb.append(']'); |
michael@0 | 94 | return sb.toString(); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | public String toJSONString(){ |
michael@0 | 98 | return toJSONString(this); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | public String toString() { |
michael@0 | 102 | return toJSONString(); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | |
michael@0 | 106 | |
michael@0 | 107 | } |