json-simple/vendor/json-simple-1.1.1/src/main/java/org/json/simple/JSONArray.java

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