michael@0: /* michael@0: * $Id: JSONObject.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.HashMap; michael@0: import java.util.Iterator; michael@0: import java.util.Map; michael@0: michael@0: /** michael@0: * A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface. michael@0: * michael@0: * @author FangYidong michael@0: */ michael@0: public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware{ michael@0: private static final long serialVersionUID = -503443796854799292L; michael@0: michael@0: /** michael@0: * Encode a map into JSON text and write it to out. michael@0: * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware 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 map michael@0: * @param out michael@0: */ michael@0: public static void writeJSONString(Map map, Writer out) throws IOException { michael@0: if(map == null){ michael@0: out.write("null"); michael@0: return; michael@0: } michael@0: michael@0: boolean first = true; michael@0: Iterator iter=map.entrySet().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: Map.Entry entry=(Map.Entry)iter.next(); michael@0: out.write('\"'); michael@0: out.write(escape(String.valueOf(entry.getKey()))); michael@0: out.write('\"'); michael@0: out.write(':'); michael@0: JSONValue.writeJSONString(entry.getValue(), 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 map to JSON text. The result is a JSON object. michael@0: * If this map 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 map michael@0: * @return JSON text, or "null" if map is null. michael@0: */ michael@0: public static String toJSONString(Map map){ michael@0: if(map == null) michael@0: return "null"; michael@0: michael@0: StringBuffer sb = new StringBuffer(); michael@0: boolean first = true; michael@0: Iterator iter=map.entrySet().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: Map.Entry entry=(Map.Entry)iter.next(); michael@0: toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb); 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: private static String toJSONString(String key,Object value, StringBuffer sb){ michael@0: sb.append('\"'); michael@0: if(key == null) michael@0: sb.append("null"); michael@0: else michael@0: JSONValue.escape(key, sb); michael@0: sb.append('\"').append(':'); michael@0: michael@0: sb.append(JSONValue.toJSONString(value)); michael@0: michael@0: return sb.toString(); michael@0: } michael@0: michael@0: public String toString(){ michael@0: return toJSONString(); michael@0: } michael@0: michael@0: public static String toString(String key,Object value){ michael@0: StringBuffer sb = new StringBuffer(); michael@0: toJSONString(key, value, sb); michael@0: return sb.toString(); michael@0: } michael@0: michael@0: /** michael@0: * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). michael@0: * It's the same as JSONValue.escape() only for compatibility here. michael@0: * michael@0: * @see org.json.simple.JSONValue#escape(String) michael@0: * michael@0: * @param s michael@0: * @return michael@0: */ michael@0: public static String escape(String s){ michael@0: return JSONValue.escape(s); michael@0: } michael@0: }