JsonUtils { public static Optional<JsonNode> convertFromStringToJSONNode(String json) { if (json == null || json.isEmpty()) { return Optional.empty(); } try { ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(json); return Optional.of(jsonNode); } catch (JsonParseException e) { return Optional.empty(); } catch (IOException e) { throw new IllegalArgumentException("Generic error during json parsing: " + json, e); } } private  JsonUtils(); static Optional<JsonNode> convertFromStringToJSONNode(String json);  }
@Test public void convertFromStringToJSONNode() { assertFalse(JsonUtils.convertFromStringToJSONNode(null).isPresent()); assertFalse(JsonUtils.convertFromStringToJSONNode("Not json").isPresent()); assertFalse(JsonUtils.convertFromStringToJSONNode("\"Not json").isPresent()); assertFalse(JsonUtils.convertFromStringToJSONNode("key : notJson\"").isPresent()); assertFalse(JsonUtils.convertFromStringToJSONNode("[key : 100]").isPresent()); assertFalse(JsonUtils.convertFromStringToJSONNode("{\"key\" : 100{").isPresent()); assertFalse(JsonUtils.convertFromStringToJSONNode("{key : 100}").isPresent()); assertTrue(JsonUtils.convertFromStringToJSONNode("\"Json\"").isPresent()); assertTrue(JsonUtils.convertFromStringToJSONNode("\"key : Json\"").isPresent()); assertTrue(JsonUtils.convertFromStringToJSONNode("{ \"id\": 2, \"username\": \"user\", \"num\": 12, \"name\": \"Mr Yellow\"\n }").isPresent()); assertTrue(JsonUtils.convertFromStringToJSONNode("{ \"users\": [\n" + "\t\t{ \"id\": 3, \"username\": \"user45\", \"num\": 24, \"name\": \"Mr White\" },\n" + "\t\t{ \"id\": 4, \"username\": \"user65\", \"num\": 32, \"name\": \"Mr Red\" }\n" + "\t]}").isPresent()); assertTrue(JsonUtils.convertFromStringToJSONNode("[{\"name\": \"\\\"John\\\"\"}, " + "{\"name\": \"\\\"John\\\"\", \"names\" : [{\"value\": \"\\\"Anna\\\"\"}, {\"value\": \"\\\"Mario\\\"\"}]}]").isPresent()); assertTrue(JsonUtils.convertFromStringToJSONNode("[1,2,3]").isPresent()); assertTrue(JsonUtils.convertFromStringToJSONNode("{\"id\": 23, \"num\": 34, \"time\" : 56}").isPresent()); assertTrue("Combine three data types in object", JsonUtils.convertFromStringToJSONNode("{\"married\":true, \"num\":34, \"name\": \"john\"}").isPresent()); assertTrue("Combine three data types in array", JsonUtils.convertFromStringToJSONNode("[{\"married\":true,\"num\":34,\"name\":\"john\"}," + "{\"married\":false,\"num\":43,\"name\":\"jane\"}]").isPresent()); assertTrue("Whitespaces", JsonUtils.convertFromStringToJSONNode("{\"is married\":\"yes, is\"}").isPresent()); }