You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2018/07/31 13:22:38 UTC

johnzon git commit: JOHNZON-183 JsonParser#hasNext() returns true even when input is completely empty

Repository: johnzon
Updated Branches:
  refs/heads/master f41430773 -> 4e8b6524a


JOHNZON-183 JsonParser#hasNext() returns true even when input is completely empty


Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/4e8b6524
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/4e8b6524
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/4e8b6524

Branch: refs/heads/master
Commit: 4e8b6524a010de3efbe5e54eec9dd6fa2140f510
Parents: f414307
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Tue Jul 31 15:22:33 2018 +0200
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Tue Jul 31 15:22:33 2018 +0200

----------------------------------------------------------------------
 .../johnzon/core/JsonStreamParserImpl.java      | 23 ++++++++++++++------
 .../org/apache/johnzon/core/JsonParserTest.java | 17 ++++++++++-----
 2 files changed, 28 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/johnzon/blob/4e8b6524/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
index 4d0571a..cd6022e 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
@@ -198,13 +198,17 @@ public class JsonStreamParserImpl extends JohnzonJsonParserImpl implements JsonC
 
     @Override
     public final boolean hasNext() {
-
-        if (currentStructureElement != null ||
-                (previousEvent != END_ARRAY && previousEvent != END_OBJECT &&
-                        previousEvent != VALUE_STRING && previousEvent != VALUE_FALSE && previousEvent != VALUE_TRUE &&
-                        previousEvent != VALUE_NULL && previousEvent != VALUE_NUMBER) ||
-                previousEvent == 0) {
-
+        if (currentStructureElement != null || previousEvent == 0) {
+            return true;
+        }
+        if (previousEvent != END_ARRAY && previousEvent != END_OBJECT &&
+                previousEvent != VALUE_STRING && previousEvent != VALUE_FALSE && previousEvent != VALUE_TRUE &&
+                previousEvent != VALUE_NULL && previousEvent != VALUE_NUMBER) {
+            if (bufferPos == Integer.MIN_VALUE) { // check we don't have an empty string to parse
+                final char c = readNextChar();
+                bufferPos--;
+                return c != EOF;
+            }
             return true;
         }
 
@@ -357,6 +361,11 @@ public class JsonStreamParserImpl extends JohnzonJsonParserImpl implements JsonC
         //main entry, make decision how to handle the current character in the stream
 
         if (!hasNext()) {
+            final char c = readNextChar();
+            bufferPos--;
+            if (c != EOF) {
+                throw uexc("No available event");
+            }
             throw new NoSuchElementException();
         }
 

http://git-wip-us.apache.org/repos/asf/johnzon/blob/4e8b6524/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
index 9436e5a..fb84307 100644
--- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
+++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
@@ -67,6 +67,13 @@ public class JsonParserTest {
     }
 
     @Test
+    public void emptyObject() {
+        final JsonParser parser = Json.createParser(new StringReader(""));
+        assertFalse(parser.hasNext());
+        parser.close();
+    }
+
+    @Test
     public void testParseGetObject() throws Exception {
         String json = "{\"a\":1,\"b\":2 }";
         JsonParser parser = Json.createParser(new StringReader(json));
@@ -991,14 +998,14 @@ public class JsonParserTest {
         Json.createReader(new ByteArrayInputStream("{\"z\":nulll}".getBytes())).read();
     }
     
-    @Test(expected = JsonException.class)
+    @Test(expected = IllegalStateException.class)
     public void zeroByteInput() {
         // using a reader as wrapper of parser
   
         Json.createReader(new ByteArrayInputStream(new byte[]{})).read();
     }
     
-    @Test(expected = JsonParsingException.class)
+    @Test(expected = IllegalStateException.class)
     public void zeroCharInput() {
         // using a reader as wrapper of parser
   
@@ -1142,13 +1149,13 @@ public class JsonParserTest {
         try {
             parser.next();
             fail();
-        } catch (JsonParsingException e) {
+        } catch (NoSuchElementException e) {
             //expected
         }
        
     }
     
-    @Test(expected=JsonParsingException.class)
+    @Test(expected=NoSuchElementException.class)
     public void testBinaryNullStream() {
         ByteArrayInputStream bin = new ByteArrayInputStream("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".getBytes(UTF_8));
         JsonParser parser = Json.createParser(bin);
@@ -1327,7 +1334,7 @@ public class JsonParserTest {
        JsonParser parser = Json.createParser(new ByteArrayInputStream("[]".getBytes()));
         assertEquals(Event.START_ARRAY, parser.next());
         assertEquals(Event.END_ARRAY, parser.next());
-        assertEquals(false, parser.hasNext());
+        assertFalse(parser.hasNext());
         try {
             parser.next();
             fail("Should have thrown a NoSuchElementException");


Fwd: johnzon git commit: JOHNZON-183 JsonParser#hasNext() returns true even when input is completely empty

Posted by Romain Manni-Bucau <rm...@apache.org>.
Hi guys,

can you have a quick look to the exceptions, I changed a few to handle the
related ticket and i'm not sure the previous tests were right or if we need
to not use hasNext() in next() to handle it.

---------- Forwarded message ---------
From: <rm...@apache.org>
Date: mar. 31 juil. 2018 à 15:22
Subject: johnzon git commit: JOHNZON-183 JsonParser#hasNext() returns true
even when input is completely empty
To: <co...@johnzon.apache.org>


Repository: johnzon
Updated Branches:
  refs/heads/master f41430773 -> 4e8b6524a


JOHNZON-183 JsonParser#hasNext() returns true even when input is completely
empty


Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/4e8b6524
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/4e8b6524
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/4e8b6524

Branch: refs/heads/master
Commit: 4e8b6524a010de3efbe5e54eec9dd6fa2140f510
Parents: f414307
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Tue Jul 31 15:22:33 2018 +0200
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Tue Jul 31 15:22:33 2018 +0200

----------------------------------------------------------------------
 .../johnzon/core/JsonStreamParserImpl.java      | 23 ++++++++++++++------
 .../org/apache/johnzon/core/JsonParserTest.java | 17 ++++++++++-----
 2 files changed, 28 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/johnzon/blob/4e8b6524/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
----------------------------------------------------------------------
diff --git
a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
index 4d0571a..cd6022e 100644
---
a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
+++
b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonStreamParserImpl.java
@@ -198,13 +198,17 @@ public class JsonStreamParserImpl extends
JohnzonJsonParserImpl implements JsonC

     @Override
     public final boolean hasNext() {
-
-        if (currentStructureElement != null ||
-                (previousEvent != END_ARRAY && previousEvent != END_OBJECT
&&
-                        previousEvent != VALUE_STRING && previousEvent !=
VALUE_FALSE && previousEvent != VALUE_TRUE &&
-                        previousEvent != VALUE_NULL && previousEvent !=
VALUE_NUMBER) ||
-                previousEvent == 0) {
-
+        if (currentStructureElement != null || previousEvent == 0) {
+            return true;
+        }
+        if (previousEvent != END_ARRAY && previousEvent != END_OBJECT &&
+                previousEvent != VALUE_STRING && previousEvent !=
VALUE_FALSE && previousEvent != VALUE_TRUE &&
+                previousEvent != VALUE_NULL && previousEvent !=
VALUE_NUMBER) {
+            if (bufferPos == Integer.MIN_VALUE) { // check we don't have
an empty string to parse
+                final char c = readNextChar();
+                bufferPos--;
+                return c != EOF;
+            }
             return true;
         }

@@ -357,6 +361,11 @@ public class JsonStreamParserImpl extends
JohnzonJsonParserImpl implements JsonC
         //main entry, make decision how to handle the current character in
the stream

         if (!hasNext()) {
+            final char c = readNextChar();
+            bufferPos--;
+            if (c != EOF) {
+                throw uexc("No available event");
+            }
             throw new NoSuchElementException();
         }


http://git-wip-us.apache.org/repos/asf/johnzon/blob/4e8b6524/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
----------------------------------------------------------------------
diff --git
a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
index 9436e5a..fb84307 100644
--- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
+++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonParserTest.java
@@ -67,6 +67,13 @@ public class JsonParserTest {
     }

     @Test
+    public void emptyObject() {
+        final JsonParser parser = Json.createParser(new StringReader(""));
+        assertFalse(parser.hasNext());
+        parser.close();
+    }
+
+    @Test
     public void testParseGetObject() throws Exception {
         String json = "{\"a\":1,\"b\":2 }";
         JsonParser parser = Json.createParser(new StringReader(json));
@@ -991,14 +998,14 @@ public class JsonParserTest {
         Json.createReader(new
ByteArrayInputStream("{\"z\":nulll}".getBytes())).read();
     }

-    @Test(expected = JsonException.class)
+    @Test(expected = IllegalStateException.class)
     public void zeroByteInput() {
         // using a reader as wrapper of parser

         Json.createReader(new ByteArrayInputStream(new byte[]{})).read();
     }

-    @Test(expected = JsonParsingException.class)
+    @Test(expected = IllegalStateException.class)
     public void zeroCharInput() {
         // using a reader as wrapper of parser

@@ -1142,13 +1149,13 @@ public class JsonParserTest {
         try {
             parser.next();
             fail();
-        } catch (JsonParsingException e) {
+        } catch (NoSuchElementException e) {
             //expected
         }

     }

-    @Test(expected=JsonParsingException.class)
+    @Test(expected=NoSuchElementException.class)
     public void testBinaryNullStream() {
         ByteArrayInputStream bin = new
ByteArrayInputStream("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".getBytes(UTF_8));
         JsonParser parser = Json.createParser(bin);
@@ -1327,7 +1334,7 @@ public class JsonParserTest {
        JsonParser parser = Json.createParser(new
ByteArrayInputStream("[]".getBytes()));
         assertEquals(Event.START_ARRAY, parser.next());
         assertEquals(Event.END_ARRAY, parser.next());
-        assertEquals(false, parser.hasNext());
+        assertFalse(parser.hasNext());
         try {
             parser.next();
             fail("Should have thrown a NoSuchElementException");