JSONUtils { public static JSONObject fromMap(Map<String, String> map) throws JSONException { JSONObject json = new JSONObject(); if (map == null) return json; for (Entry<String, String> entry : map.entrySet()) { json.put(entry.getKey(), entry.getValue()); } return json; } private  JSONUtils(); static JObject mapToJSON(Map<String, String> map); static JValue setToJSON(Set<String> set); static JValue filtersToJSON(ResourceListQuery query, ListProvidersService listProvidersService,
          Organization org); static JValue filtersToJSONSeriesWriteAccess(ResourceListQuery query, ListProvidersService listProvidersService,
          Organization org, Map<String, String> series); static JSONObject fromMap(Map<String, String> map); static Map<String, String> toMap(JSONObject json);  }
@Test public void testFromMap() throws Exception { Map<String, String> map = new HashMap<String, String>(); JSONObject json; map.put("key", "value"); map.put("key with spaces", "value"); json = JSONUtils.fromMap(map); assertEquals("value", json.getString("key")); assertEquals("value", json.getString("key with spaces")); }
@Test public void testFromMapWithNull() throws Exception { assertEquals(0, JSONUtils.fromMap(null).length()); }
@Test(expected = JSONException.class) public void testFromMapWithNullKey() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put(null, "value"); JSONUtils.fromMap(map); }