@Test void testExcludeNullEmpty() { Map<String, Object> map = new HashMap<>(); map.put("a", null); map.put("b", new HashMap<>()); String json = new JsonSerializer().excludeNulls(true).serialize(map); assertEquals("{\"b\":{}}", json); json = new JsonSerializer().excludeNulls(true).excludeEmpty(true).serialize(map); assertEquals("{}", json); map.put("c", new ArrayList<>()); json = new JsonSerializer().excludeNulls(true).excludeEmpty(true).serialize(map); assertEquals("{}", json); map.put("d", new int[0]); json = new JsonSerializer().excludeNulls(true).excludeEmpty(true).serialize(map); assertEquals("{}", json); map.put("e", new HashSet<>()); json = new JsonSerializer().excludeNulls(true).excludeEmpty(true).serialize(map); assertEquals("{}", json); map.put("f", new Object[0]); json = new JsonSerializer().excludeNulls(true).excludeEmpty(true).serialize(map); assertEquals("{}", json); }
@Test void testExcludeNullCollections() { Map<String, Object> map = new HashMap<>(); map.put("a", null); String json = new JsonSerializer().serialize(map); assertEquals("{\"a\":null}", json); json = new JsonSerializer().excludeNulls(true).serialize(map); assertEquals("{}", json); map.put("b", new HashMap<>()); json = new JsonSerializer().excludeNulls(true).serialize(map); assertEquals("{\"b\":{}}", json); json = new JsonSerializer().excludeNulls(true).onValue(value -> { if (value instanceof Map) { if (((Map)value).isEmpty()) { return new EmptyJsonSerializer(); } } return null; }).serialize(map); assertEquals("{}", json); }
@Test void testExcludingNulls() { Map<String, Object> map = new HashMap<>(); map.put("one", null); String json = new JsonSerializer().serialize(map); assertEquals("{\"one\":null}", json); json = new JsonSerializer().excludeNulls(true).serialize(map); assertEquals("{}", json); State state = new State(); map.put("one", state); json = new JsonSerializer().serialize(map); assertTrue(json.startsWith("{\"one\":")); json = new JsonSerializer().excludeNulls(true).serialize(map); assertEquals("{\"one\":{}}", json); state.setId(1); json = new JsonSerializer().excludeNulls(true).serialize(map); assertEquals("{\"one\":{\"id\":1}}", json); }