johnzon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [johnzon] csaboka commented on a change in pull request #74: True object and array streaming
Date Sat, 10 Jul 2021 13:11:38 GMT

csaboka commented on a change in pull request #74:
URL: https://github.com/apache/johnzon/pull/74#discussion_r667337818



##########
File path: johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserStreamingTest.java
##########
@@ -121,4 +127,59 @@ private String parserAndConcat(String json) {
                 .collect(Collectors.joining(","));
         return sum;
     }
+
+    @Test
+    public void testGetArrayStream() {
+        StringReader sr = new StringReader("[1,2,3,4,5,6]");
+        JsonParser jsonParser = Json.createParser(sr);
+
+        JsonParser.Event firstEvent = jsonParser.next();
+        assertEquals(JsonParser.Event.START_ARRAY, firstEvent);
+
+        int sum = jsonParser.getArrayStream()
+                .mapToInt(v -> ((JsonNumber)v).intValue())
+                .sum();
+        assertEquals(21, sum);
+    }
+
+    @Test(expected = JsonParsingException.class)
+    public void testParseErrorInGetArrayStream() {
+        StringReader sr = new StringReader("[\"this is\":\"not an object\"]");
+        JsonParser jsonParser = Json.createParser(sr);
+
+        JsonParser.Event firstEvent = jsonParser.next();
+        assertEquals(JsonParser.Event.START_ARRAY, firstEvent);
+
+        jsonParser.getArrayStream().forEach(dummy -> {});
+    }
+
+    @Test
+    public void testGetObjectStream() {
+        StringReader sr = new StringReader("{\"foo\":\"bar\",\"baz\":\"quux\",\"something\":\"else\"}");
+        JsonParser jsonParser = Json.createParser(sr);
+
+        JsonParser.Event firstEvent = jsonParser.next();
+        assertEquals(JsonParser.Event.START_OBJECT, firstEvent);
+
+        Map<String, String> mappings = jsonParser.getObjectStream()
+                .collect(Collectors.toMap(Map.Entry::getKey, e -> ((JsonString)e.getValue()).getString()));
+
+        Map<String, String> expectedMappings = new HashMap<>();
+        expectedMappings.put("foo", "bar");
+        expectedMappings.put("baz", "quux");
+        expectedMappings.put("something", "else");
+        assertEquals(expectedMappings, mappings);
+    }
+
+    @Test(expected = JsonParsingException.class)
+    public void testParseErrorInGetObjectStream() {
+        StringReader sr = new StringReader("{42}");
+        JsonParser jsonParser = Json.createParser(sr);
+
+        JsonParser.Event firstEvent = jsonParser.next();

Review comment:
       None of the other tests closed the parser, that's why I assumed it's fine. I've changed
now so the parser is closed not just in the new tests, but the existing ones as well.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@johnzon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message