@SuppressWarnings("unchecked") @Test public void testBasic() { MapJsonRenderer renderer = new MapJsonRenderer(false); JSONStringer jsonWriter = new JSONStringer(); jsonWriter.addRenderer(renderer); Map<String, Object> map = new LinkedHashMap<String, Object>(); jsonWriter.value(map); assertEquals(jsonWriter.toString(), "{}"); jsonWriter = new JSONStringer(); jsonWriter.addRenderer(renderer); map.put("foo", true); // check recursion map.put("bar", new LinkedHashMap()); map.put("str", "\'\":);"); jsonWriter.value(map); assertEquals(jsonWriter.toString(), "{\"foo\":true,\"bar\":{},\"str\":\"'\\\":);\"}"); }
/** allow null values but not null keys */ @Test public void testAllowNulls() { MapJsonRenderer renderer = new MapJsonRenderer(true); JSONStringer jsonWriter = new JSONStringer(); jsonWriter.addRenderer(renderer); Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("ff", null); jsonWriter.value(map); assertEquals(jsonWriter.toString(), "{\"ff\":null}"); }
@Test public void testNullFiltering() { MapJsonRenderer renderer = new MapJsonRenderer(false); JSONStringer jsonWriter = new JSONStringer(); jsonWriter.addRenderer(renderer); Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put(null, "gg"); map.put("ff", null); jsonWriter.value(map); assertEquals(jsonWriter.toString(), "{}"); }