johnzon-dev mailing list archives

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

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



##########
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:
       guess we should use a try with resource pattern to close the parser (it is required
even for string readers due to memory management, even if not critical in tests it is always
better to not show antipatterns in tests since they are kind of part of the doc)
   if you test the message it looks ok.
   
   But looks sufficient for me as coverage.




-- 
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