JSONTool { public String serialize(Object object) { try { ObjectMapper mapper = new ObjectMapper(); SimpleModule m = new SimpleModule("org.json.* serializer", new Version(1, 0, 0, "", "org.json", "json")); m.addSerializer(JSONObject.class, new JSONObjectSerializer()); m.addSerializer(JSONArray.class, new JSONArraySerializer()); mapper.registerModule(m); return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { LOGGER.error("Failed to serialize object to JSON", e); } return null; }  String serialize(Object object); Object fromString(String str); @Deprecated JSON parse(String json);  }
@Test void serializeNull() { assertEquals("null", this.tool.serialize(null)); }
@Test void serializeMap() { Map<String, Object> map = new HashMap<>(); map.put("bool", false); map.put("int", 13); map.put("double", 0.78); map.put("string", "foo"); map.put("array", new int[] { 9, 8 }); map.put("list", Arrays.asList("one", "two")); map.put("map", Collections.singletonMap("level2", true)); map.put("null", "null key value"); String json = this.tool.serialize(map); assertTrue(json.contains("\"bool\":false")); assertTrue(json.contains("\"int\":13")); assertTrue(json.contains("\"double\":0.78")); assertTrue(json.contains("\"string\":\"foo\"")); assertTrue(json.contains("\"array\":[9,8]")); assertTrue(json.contains("\"list\":[\"one\",\"two\"]")); assertTrue(json.contains("\"map\":{\"level2\":true}")); assertTrue(json.contains("\"null\":\"null key value\"")); }
@Test void serializeList() { assertEquals("[1,2]", this.tool.serialize(Arrays.asList(1, 2))); assertEquals("[1.3,2.4]", this.tool.serialize(new double[] { 1.3, 2.4 })); }
@Test void serializeNumber() { assertEquals("27", this.tool.serialize(27)); assertEquals("2.7", this.tool.serialize(2.7)); }
@Test void serializeBoolean() { assertEquals("false", this.tool.serialize(false)); assertEquals("true", this.tool.serialize(true)); }
@Test void serializeString() { assertEquals("\"\\\"te'st\\\"\"", this.tool.serialize("\"te'st\"")); }
@Test void serializeBean() { String json = this.tool.serialize(new MockBean()); assertTrue(json.contains("\"age\":28")); assertTrue(json.contains("\"enabled\":true")); assertTrue(json.contains("\"grade\":9.48")); assertTrue(json.contains("\"items\":[\"one\"]")); assertTrue(json.contains("\"name\":\"XWiki\"")); assertTrue(json.contains("\"parameters\":{\"foo\":\"bar\"}")); assertFalse(json.contains("\"transientProperty\":\"transient\"")); }
@Test void serializeOrgJsonObjectWorks() { List<String> variants = new ArrayList<>(2); variants.add("{\"a\":\"b\",\"c\":true}"); variants.add("{\"c\":true,\"a\":\"b\"}"); org.json.JSONObject object = new org.json.JSONObject(); object.put("a", "b"); object.put("c", true); assertTrue(variants.contains(this.tool.serialize(object))); }
@Test void serializeNestedOrgJsonObjectWorks() { List<String> variants = new ArrayList<>(2); variants.add("{\"before\":[\"nothing\"],\"json\":{\"a\":\"b\",\"c\":true},\"after\":42}"); variants.add("{\"before\":[\"nothing\"],\"json\":{\"c\":true,\"a\":\"b\"},\"after\":42}"); org.json.JSONObject object = new org.json.JSONObject(); object.put("a", "b"); object.put("c", true); Map<String, Object> map = new LinkedHashMap<>(); map.put("before", Collections.singletonList("nothing")); map.put("json", object); map.put("after", 42); assertTrue(variants.contains(this.tool.serialize(map))); }
@Test void serializeOrgJsonArrayWorks() { org.json.JSONArray array = new org.json.JSONArray(); array.put("a"); array.put(42); array.put(true); assertEquals("[\"a\",42,true]", this.tool.serialize(array)); }
@Test void serializeNestedOrgJsonArrayWorks() { org.json.JSONArray array = new org.json.JSONArray(); array.put("a"); array.put(42); array.put(true); Map<String, Object> map = new LinkedHashMap<>(); map.put("before", Collections.singletonList("nothing")); map.put("json", array); map.put("after", 42); assertEquals("{\"before\":[\"nothing\"],\"json\":[\"a\",42,true],\"after\":42}", this.tool.serialize(map)); }