A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface. @author FangYidong
| 17 | * @author FangYidong<fangyidong@yahoo.com.cn> |
| 18 | */ |
| 19 | public class JSONObject extends HashMap<Object, Object> implements Map<Object, Object>, JSONAware, JSONStreamAware{ |
| 20 | private static final long serialVersionUID = -503443796854799292L; |
| 21 | |
| 22 | /** |
| 23 | * Encode a map into JSON text and write it to out. |
| 24 | * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level. |
| 25 | * |
| 26 | * @see org.json.simpleForBukkit.JSONValue#writeJSONString(Object, Writer) |
| 27 | * |
| 28 | * @param map |
| 29 | * @param out |
| 30 | */ |
| 31 | @SuppressWarnings("unchecked") |
| 32 | public static void writeJSONString(Map map, Writer out) throws IOException { |
| 33 | if(map == null){ |
| 34 | out.write("null"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | boolean first = true; |
| 39 | Iterator<Object> iter = map.entrySet().iterator(); |
| 40 | |
| 41 | out.write('{'); |
| 42 | while(iter.hasNext()){ |
| 43 | if(first) |
| 44 | first = false; |
| 45 | else |
| 46 | out.write(','); |
| 47 | Map.Entry<Object, Object> entry=(Map.Entry<Object, Object>)iter.next(); |
| 48 | out.write('\"'); |
| 49 | out.write(escape(String.valueOf(entry.getKey()))); |
| 50 | out.write('\"'); |
| 51 | out.write(':'); |
| 52 | JSONValue.writeJSONString(entry.getValue(), out); |
| 53 | } |
| 54 | out.write('}'); |
| 55 | } |
| 56 | |
| 57 | public void writeJSONString(Writer out) throws IOException{ |
| 58 | writeJSONString(this, out); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Convert a map to JSON text. The result is a JSON object. |
| 63 | * If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level. |
| 64 | * |
| 65 | * @see org.json.simpleForBukkit.JSONValue#toJSONString(Object) |
| 66 | * |
| 67 | * @param map |
| 68 | * @return JSON text, or "null" if map is null. |
| 69 | */ |
| 70 | public static String toJSONString(Map<Object, Object> map){ |
| 71 | if(map == null) |
| 72 | return "null"; |
| 73 | |
| 74 | StringBuffer sb = new StringBuffer(); |
| 75 | boolean first = true; |
| 76 | Iterator<Entry<Object, Object>> iter=map.entrySet().iterator(); |
nothing calls this directly
no outgoing calls
no test coverage detected