Methods Summary |
---|
private static java.lang.Object | coerce(java.lang.Object value)
if ((value instanceof Map) && !(value instanceof JSONObject)) {
value = encodeToJSONObject((Map)value);
} else if ((value instanceof List) && !(value instanceof JSONArray)) {
value = encodeToJSONArray((List)value);
} else if (value instanceof Object[]) {
Object[] array = (Object[])value;
value = encodeToJSONArray(Arrays.asList(array));
}
return value;
|
public static java.util.Map | decodeJSON(java.lang.String json)decodes JSON formatted text into a map.
try {
Object object = JSONValue.parse(json);
if (object instanceof Map) {
return (Map) object;
}
// could be : ArrayList, String, Number, Boolean
Map map = new HashMap();
map.put("value", object);
return map;
} catch (Throwable t) {
Debug.out("Warning: Bad JSON String: " + json, t);
return null;
}
|
public static java.lang.String | encodeToJSON(java.util.Map map)Encodes a map into a JSON formatted string.
Handles multiple layers of Maps and Lists. Handls String, Number,
Boolean, and null values.
return encodeToJSONObject(map).toString();
|
public static java.lang.String | encodeToJSON(java.util.Collection list)
return encodeToJSONArray(list).toString();
|
private static java.util.List | encodeToJSONArray(java.util.Collection list)
List newList = new JSONArray(list);
for (int i = 0; i < newList.size(); i++) {
Object value = newList.get(i);
newList.set(i, coerce(value));
}
return newList;
|
public static java.util.Map | encodeToJSONObject(java.util.Map map)encodes a map into a JSONObject.
It's recommended that you use {@link #encodeToJSON(Map)} instead
Map newMap = new JSONObject();
for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
Object key = (Object) iter.next();
Object value = map.get(key);
value = coerce(value);
newMap.put(key, value);
}
return newMap;
|
public static void | main(java.lang.String[] args)
Map map = new HashMap();
map.put("Test", "TestValue");
Map map2 = new HashMap();
map2.put("Test2", "test2value");
map.put("TestMap", map2);
List list = new ArrayList();
list.add(new Long(5));
list.add("five");
map2.put("ListTest", list);
Map map3 = new HashMap();
map3.put("Test3", "test3value");
list.add(map3);
System.out.println(encodeToJSON(map));
System.out.println(encodeToJSON(list));
|