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

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * $Id: JSONObject.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.HashMap;
michael@0 10 import java.util.Iterator;
michael@0 11 import java.util.Map;
michael@0 12
michael@0 13 /**
michael@0 14 * A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface.
michael@0 15 *
michael@0 16 * @author FangYidong<fangyidong@yahoo.com.cn>
michael@0 17 */
michael@0 18 public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware{
michael@0 19 private static final long serialVersionUID = -503443796854799292L;
michael@0 20
michael@0 21 /**
michael@0 22 * Encode a map into JSON text and write it to out.
michael@0 23 * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level.
michael@0 24 *
michael@0 25 * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
michael@0 26 *
michael@0 27 * @param map
michael@0 28 * @param out
michael@0 29 */
michael@0 30 public static void writeJSONString(Map map, Writer out) throws IOException {
michael@0 31 if(map == null){
michael@0 32 out.write("null");
michael@0 33 return;
michael@0 34 }
michael@0 35
michael@0 36 boolean first = true;
michael@0 37 Iterator iter=map.entrySet().iterator();
michael@0 38
michael@0 39 out.write('{');
michael@0 40 while(iter.hasNext()){
michael@0 41 if(first)
michael@0 42 first = false;
michael@0 43 else
michael@0 44 out.write(',');
michael@0 45 Map.Entry entry=(Map.Entry)iter.next();
michael@0 46 out.write('\"');
michael@0 47 out.write(escape(String.valueOf(entry.getKey())));
michael@0 48 out.write('\"');
michael@0 49 out.write(':');
michael@0 50 JSONValue.writeJSONString(entry.getValue(), out);
michael@0 51 }
michael@0 52 out.write('}');
michael@0 53 }
michael@0 54
michael@0 55 public void writeJSONString(Writer out) throws IOException{
michael@0 56 writeJSONString(this, out);
michael@0 57 }
michael@0 58
michael@0 59 /**
michael@0 60 * Convert a map to JSON text. The result is a JSON object.
michael@0 61 * If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
michael@0 62 *
michael@0 63 * @see org.json.simple.JSONValue#toJSONString(Object)
michael@0 64 *
michael@0 65 * @param map
michael@0 66 * @return JSON text, or "null" if map is null.
michael@0 67 */
michael@0 68 public static String toJSONString(Map map){
michael@0 69 if(map == null)
michael@0 70 return "null";
michael@0 71
michael@0 72 StringBuffer sb = new StringBuffer();
michael@0 73 boolean first = true;
michael@0 74 Iterator iter=map.entrySet().iterator();
michael@0 75
michael@0 76 sb.append('{');
michael@0 77 while(iter.hasNext()){
michael@0 78 if(first)
michael@0 79 first = false;
michael@0 80 else
michael@0 81 sb.append(',');
michael@0 82
michael@0 83 Map.Entry entry=(Map.Entry)iter.next();
michael@0 84 toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb);
michael@0 85 }
michael@0 86 sb.append('}');
michael@0 87 return sb.toString();
michael@0 88 }
michael@0 89
michael@0 90 public String toJSONString(){
michael@0 91 return toJSONString(this);
michael@0 92 }
michael@0 93
michael@0 94 private static String toJSONString(String key,Object value, StringBuffer sb){
michael@0 95 sb.append('\"');
michael@0 96 if(key == null)
michael@0 97 sb.append("null");
michael@0 98 else
michael@0 99 JSONValue.escape(key, sb);
michael@0 100 sb.append('\"').append(':');
michael@0 101
michael@0 102 sb.append(JSONValue.toJSONString(value));
michael@0 103
michael@0 104 return sb.toString();
michael@0 105 }
michael@0 106
michael@0 107 public String toString(){
michael@0 108 return toJSONString();
michael@0 109 }
michael@0 110
michael@0 111 public static String toString(String key,Object value){
michael@0 112 StringBuffer sb = new StringBuffer();
michael@0 113 toJSONString(key, value, sb);
michael@0 114 return sb.toString();
michael@0 115 }
michael@0 116
michael@0 117 /**
michael@0 118 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
michael@0 119 * It's the same as JSONValue.escape() only for compatibility here.
michael@0 120 *
michael@0 121 * @see org.json.simple.JSONValue#escape(String)
michael@0 122 *
michael@0 123 * @param s
michael@0 124 * @return
michael@0 125 */
michael@0 126 public static String escape(String s){
michael@0 127 return JSONValue.escape(s);
michael@0 128 }
michael@0 129 }

mercurial