FileDocCategorySizeDatePackage
JSONUtils.javaAPI DocAzureus 3.0.3.44088Sat Jun 23 14:05:32 BST 2007com.aelitis.azureus.util

JSONUtils

public class JSONUtils extends Object
author
TuxPaper
created
Feb 14, 2007

Fields Summary
Constructors Summary
Methods Summary
private static java.lang.Objectcoerce(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.MapdecodeJSON(java.lang.String json)
decodes JSON formatted text into a map.

return
Map parsed from a JSON formatted string

If the json text is not a map, a map with the key "value" will be returned. the value of "value" will either be an List, String, Number, Boolean, or null

if the String is formatted badly, null is returned

		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.StringencodeToJSON(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.

param
map Map to change into a JSON formatted string
return
JSON formatted string
since
3.0.1.5

		return encodeToJSONObject(map).toString();
	
public static java.lang.StringencodeToJSON(java.util.Collection list)

		return encodeToJSONArray(list).toString();
	
private static java.util.ListencodeToJSONArray(java.util.Collection list)

param
value
return
since
3.0.1.5

		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.MapencodeToJSONObject(java.util.Map map)
encodes a map into a JSONObject.

It's recommended that you use {@link #encodeToJSON(Map)} instead

param
map
return
since
3.0.1.5

		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 voidmain(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));