X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fjson%2Fsimple%2FJSONObject.java;fp=src%2Forg%2Fjson%2Fsimple%2FJSONObject.java;h=e56091aafebe3bcf9709ae1424f6fb1da689e44a;hb=c0501eaa85c0594f9275766f64de8ea44a59c368;hp=fafa36b214ba7db29dc07439501902a6e7ca9da6;hpb=304e64fb34b32659be1bbfd39fb4e15b2f79586e;p=jalview.git diff --git a/src/org/json/simple/JSONObject.java b/src/org/json/simple/JSONObject.java index fafa36b..e56091a 100644 --- a/src/org/json/simple/JSONObject.java +++ b/src/org/json/simple/JSONObject.java @@ -12,121 +12,138 @@ import java.util.Iterator; import java.util.Map; /** - * A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface. + * A JSON object. Key value pairs are unordered. JSONObject supports + * java.util.Map interface. * * @author FangYidong */ -public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware{ - - private static final long serialVersionUID = -503443796854799292L; - - - public JSONObject() { - super(); - } - - /** - * Allows creation of a JSONObject from a Map. After that, both the - * generated JSONObject and the Map can be modified independently. - * - * @param map - */ - public JSONObject(Map map) { - super(map); - } - - - /** - * Encode a map into JSON text and write it to out. - * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level. - * - * @see org.json.simple.JSONValue#writeJSONString(Object, Writer) - * - * @param map - * @param out - */ - public static void writeJSONString(Map map, Writer out) throws IOException { - if(map == null){ - out.write("null"); - return; - } - - boolean first = true; - Iterator iter=map.entrySet().iterator(); - - out.write('{'); - while(iter.hasNext()){ - if(first) - first = false; - else - out.write(','); - Map.Entry entry=(Map.Entry)iter.next(); - out.write('\"'); - out.write(escape(String.valueOf(entry.getKey()))); - out.write('\"'); - out.write(':'); - JSONValue.writeJSONString(entry.getValue(), out); - } - out.write('}'); - } - - public void writeJSONString(Writer out) throws IOException{ - writeJSONString(this, out); - } - - /** - * Convert a map to JSON text. The result is a JSON object. - * If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level. - * - * @see org.json.simple.JSONValue#toJSONString(Object) - * - * @param map - * @return JSON text, or "null" if map is null. - */ - public static String toJSONString(Map map){ - final StringWriter writer = new StringWriter(); - - try { - writeJSONString(map, writer); - return writer.toString(); - } catch (IOException e) { - // This should never happen with a StringWriter - throw new RuntimeException(e); - } - } - - public String toJSONString(){ - return toJSONString(this); - } - - public String toString(){ - return toJSONString(); - } - - public static String toString(String key,Object value){ - StringBuffer sb = new StringBuffer(); - sb.append('\"'); - if(key == null) - sb.append("null"); - else - JSONValue.escape(key, sb); - sb.append('\"').append(':'); - - sb.append(JSONValue.toJSONString(value)); - - return sb.toString(); - } - - /** - * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). - * It's the same as JSONValue.escape() only for compatibility here. - * - * @see org.json.simple.JSONValue#escape(String) - * - * @param s - * @return - */ - public static String escape(String s){ - return JSONValue.escape(s); - } +public class JSONObject extends HashMap + implements Map, JSONAware, JSONStreamAware +{ + + private static final long serialVersionUID = -503443796854799292L; + + public JSONObject() + { + super(); + } + + /** + * Allows creation of a JSONObject from a Map. After that, both the generated + * JSONObject and the Map can be modified independently. + * + * @param map + */ + public JSONObject(Map map) + { + super(map); + } + + /** + * Encode a map into JSON text and write it to out. If this map is also a + * JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific + * behaviours will be ignored at this top level. + * + * @see org.json.simple.JSONValue#writeJSONString(Object, Writer) + * + * @param map + * @param out + */ + public static void writeJSONString(Map map, Writer out) throws IOException + { + if (map == null) + { + out.write("null"); + return; + } + + boolean first = true; + Iterator iter = map.entrySet().iterator(); + + out.write('{'); + while (iter.hasNext()) + { + if (first) + first = false; + else + out.write(','); + Map.Entry entry = (Map.Entry) iter.next(); + out.write('\"'); + out.write(escape(String.valueOf(entry.getKey()))); + out.write('\"'); + out.write(':'); + JSONValue.writeJSONString(entry.getValue(), out); + } + out.write('}'); + } + + public void writeJSONString(Writer out) throws IOException + { + writeJSONString(this, out); + } + + /** + * Convert a map to JSON text. The result is a JSON object. If this map is + * also a JSONAware, JSONAware specific behaviours will be omitted at this top + * level. + * + * @see org.json.simple.JSONValue#toJSONString(Object) + * + * @param map + * @return JSON text, or "null" if map is null. + */ + public static String toJSONString(Map map) + { + final StringWriter writer = new StringWriter(); + + try + { + writeJSONString(map, writer); + return writer.toString(); + } catch (IOException e) + { + // This should never happen with a StringWriter + throw new RuntimeException(e); + } + } + + public String toJSONString() + { + return toJSONString(this); + } + + public String toString() + { + return toJSONString(); + } + + public static String toString(String key, Object value) + { + StringBuffer sb = new StringBuffer(); + sb.append('\"'); + if (key == null) + sb.append("null"); + else + JSONValue.escape(key, sb); + sb.append('\"').append(':'); + + sb.append(JSONValue.toJSONString(value)); + + return sb.toString(); + } + + /** + * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters + * (U+0000 through U+001F). It's the same as JSONValue.escape() only for + * compatibility here. + * + * @see org.json.simple.JSONValue#escape(String) + * + * @param s + * @return + */ + public static String escape(String s) + { + return JSONValue.escape(s); + } }