@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\"")); }
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; }
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; } }
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; }  }
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); }
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);  }