Json { public static Object fromJson(String json) { try (final InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) { return fromJson(stream); } catch (final IOException ex) { throw new JsonException(ex); } } private  Json(); static String toJson(Object object); static String toJson(Object object, boolean pretty); static void toJson(Object object, OutputStream out); static void toJson(Object object, OutputStream out, boolean pretty); static Object fromJson(String json); static Object fromJson(InputStream in);  }
@Test void testParse_String() { final String json = "{\"message\":\"Hello, World!\"}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals("Hello, World!", map.get("message")); }
@Test void testParse_String2() { final String json = "{\"title\" : \"Greetings!\", \"message\" : \"Hello, World!\"}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals("Hello, World!", map.get("message")); assertEquals("Greetings!", map.get("title")); }
@Test void testParse_Long() { final String json = "{\"id\" : 5678}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals(5678L, map.get("id")); }
@Test void testParse_NegativeLong() { final String json = "{\"id\" : -5678}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals(-5678L, map.get("id")); }
@Test void testParse_Double() { final String json = "{\"average\" : 0.6789}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals(0.6789, map.get("average")); }
@Test void testParse_NegativeDouble() { final String json = "{\"average\" : -0.6789}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals(-0.6789, map.get("average")); }
@Test void testParse_False() { final String json = "{\"condition\" : false}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals(false, map.get("condition")); }
@Test void testParse_True() { final String json = "{\"condition\" : true}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals(true, map.get("condition")); }
@Test void testParse_Null() { final String json = "{\"random\" : null}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertTrue(map.containsKey("random")); assertNull(map.get("random")); }
@Test void testParse_Array() { final String json = "{\"items\" : [\"one\", \"two\", \"three\"]}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); final List<String> expected = Arrays.asList("one", "two", "three"); assertNotNull(map); assertEquals(expected, map.get("items")); }
@Test void testParse_ArrayOfObjects() { final String json = "{\"numbers\" : [{\"one\":1}, {\"two\":2}, {\"three\":3}]}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); @SuppressWarnings("unchecked") final List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("numbers"); assertEquals(1L, list.get(0).get("one")); assertEquals(2L, list.get(1).get("two")); assertEquals(3L, list.get(2).get("three")); }
@Test void testParse_ArrayOfNegativeObjects() { final String json = "{\"numbers\" : [{\"one\":-1}, {\"two\":-2}, {\"three\":-3}]}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); @SuppressWarnings("unchecked") final List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("numbers"); assertEquals(-1L, list.get(0).get("one")); assertEquals(-2L, list.get(1).get("two")); assertEquals(-3L, list.get(2).get("three")); }
@Test void testParse_EscapedString() { final String json = "{\"message\":\"Hello, \\\"World\\\"!\\n\"}"; @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) Json.fromJson(json); assertNotNull(map); assertEquals("Hello, \"World\"!\n", map.get("message")); }
@TestFactory Stream<DynamicTest> fromJson() { final Map<String, String> testCases = fromJsonTestCases(); final Map<String, String> invalidTestCases = fromJsonInvalidTestCases(); Stream<DynamicTest> validTests = testCases.entrySet().stream().map(testCase -> dynamicTest(testCase.getKey(), () -> { final String value = testCase.getValue(); assertDoesNotThrow(() -> Json.fromJson(value)); })); Stream<DynamicTest> invalidTests = invalidTestCases.entrySet().stream().map(testCase -> dynamicTest(testCase.getKey(), () -> { final String value = testCase.getValue(); assertThrows(JsonSyntaxException.class, () -> Json.fromJson(value)); })); return Stream.concat(validTests, invalidTests); }