mobile/android/thirdparty/org/json/simple/JSONObject.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/org/json/simple/JSONObject.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * $Id: JSONObject.java,v 1.1 2006/04/15 14:10:48 platform Exp $
     1.6 + * Created on 2006-4-10
     1.7 + */
     1.8 +package org.json.simple;
     1.9 +
    1.10 +import java.io.IOException;
    1.11 +import java.io.Writer;
    1.12 +import java.util.HashMap;
    1.13 +import java.util.Iterator;
    1.14 +import java.util.Map;
    1.15 +
    1.16 +/**
    1.17 + * A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface.
    1.18 + * 
    1.19 + * @author FangYidong<fangyidong@yahoo.com.cn>
    1.20 + */
    1.21 +public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware{
    1.22 +	private static final long serialVersionUID = -503443796854799292L;
    1.23 +
    1.24 +    /**
    1.25 +     * Encode a map into JSON text and write it to out.
    1.26 +     * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level.
    1.27 +     * 
    1.28 +     * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
    1.29 +     * 
    1.30 +     * @param map
    1.31 +     * @param out
    1.32 +     */
    1.33 +	public static void writeJSONString(Map map, Writer out) throws IOException {
    1.34 +		if(map == null){
    1.35 +			out.write("null");
    1.36 +			return;
    1.37 +		}
    1.38 +		
    1.39 +		boolean first = true;
    1.40 +		Iterator iter=map.entrySet().iterator();
    1.41 +		
    1.42 +        out.write('{');
    1.43 +		while(iter.hasNext()){
    1.44 +            if(first)
    1.45 +                first = false;
    1.46 +            else
    1.47 +                out.write(',');
    1.48 +			Map.Entry entry=(Map.Entry)iter.next();
    1.49 +            out.write('\"');
    1.50 +            out.write(escape(String.valueOf(entry.getKey())));
    1.51 +            out.write('\"');
    1.52 +            out.write(':');
    1.53 +			JSONValue.writeJSONString(entry.getValue(), out);
    1.54 +		}
    1.55 +		out.write('}');
    1.56 +	}
    1.57 +
    1.58 +	public void writeJSONString(Writer out) throws IOException{
    1.59 +		writeJSONString(this, out);
    1.60 +	}
    1.61 +	
    1.62 +	/**
    1.63 +	 * Convert a map to JSON text. The result is a JSON object. 
    1.64 +	 * If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
    1.65 +	 * 
    1.66 +	 * @see org.json.simple.JSONValue#toJSONString(Object)
    1.67 +	 * 
    1.68 +	 * @param map
    1.69 +	 * @return JSON text, or "null" if map is null.
    1.70 +	 */
    1.71 +	public static String toJSONString(Map map){
    1.72 +		if(map == null)
    1.73 +			return "null";
    1.74 +		
    1.75 +        StringBuffer sb = new StringBuffer();
    1.76 +        boolean first = true;
    1.77 +		Iterator iter=map.entrySet().iterator();
    1.78 +		
    1.79 +        sb.append('{');
    1.80 +		while(iter.hasNext()){
    1.81 +            if(first)
    1.82 +                first = false;
    1.83 +            else
    1.84 +                sb.append(',');
    1.85 +            
    1.86 +			Map.Entry entry=(Map.Entry)iter.next();
    1.87 +			toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb);
    1.88 +		}
    1.89 +        sb.append('}');
    1.90 +		return sb.toString();
    1.91 +	}
    1.92 +	
    1.93 +	public String toJSONString(){
    1.94 +		return toJSONString(this);
    1.95 +	}
    1.96 +	
    1.97 +	private static String toJSONString(String key,Object value, StringBuffer sb){
    1.98 +		sb.append('\"');
    1.99 +        if(key == null)
   1.100 +            sb.append("null");
   1.101 +        else
   1.102 +            JSONValue.escape(key, sb);
   1.103 +		sb.append('\"').append(':');
   1.104 +		
   1.105 +		sb.append(JSONValue.toJSONString(value));
   1.106 +		
   1.107 +		return sb.toString();
   1.108 +	}
   1.109 +	
   1.110 +	public String toString(){
   1.111 +		return toJSONString();
   1.112 +	}
   1.113 +
   1.114 +	public static String toString(String key,Object value){
   1.115 +        StringBuffer sb = new StringBuffer();
   1.116 +		toJSONString(key, value, sb);
   1.117 +        return sb.toString();
   1.118 +	}
   1.119 +	
   1.120 +	/**
   1.121 +	 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
   1.122 +	 * It's the same as JSONValue.escape() only for compatibility here.
   1.123 +	 * 
   1.124 +	 * @see org.json.simple.JSONValue#escape(String)
   1.125 +	 * 
   1.126 +	 * @param s
   1.127 +	 * @return
   1.128 +	 */
   1.129 +	public static String escape(String s){
   1.130 +		return JSONValue.escape(s);
   1.131 +	}
   1.132 +}

mercurial