Skip to content

Commit e82316a

Browse files
lukasjm0mus
authored andcommitted
fix for regression #104 (#127)
Signed-off-by: Lukas Jungmann <[email protected]>
1 parent 3ca0db0 commit e82316a

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

impl/src/main/java/org/glassfish/json/JsonParserImpl.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -282,6 +282,7 @@ public void skipArray() {
282282
if (currentEvent == Event.START_ARRAY) {
283283
currentContext.skip();
284284
currentContext = stack.pop();
285+
currentEvent = Event.END_ARRAY;
285286
}
286287
}
287288

@@ -290,6 +291,7 @@ public void skipObject() {
290291
if (currentEvent == Event.START_OBJECT) {
291292
currentContext.skip();
292293
currentContext = stack.pop();
294+
currentEvent = Event.END_OBJECT;
293295
}
294296
}
295297

@@ -328,7 +330,18 @@ public JsonLocation getLastCharLocation() {
328330

329331
@Override
330332
public boolean hasNext() {
331-
return tokenizer.hasNextToken();
333+
if (stack.isEmpty() && (currentEvent == Event.END_ARRAY || currentEvent == Event.END_OBJECT)) {
334+
JsonToken token = tokenizer.nextToken();
335+
if (token != JsonToken.EOF) {
336+
throw new JsonParsingException(JsonMessages.PARSER_EXPECTED_EOF(token),
337+
getLastCharLocation());
338+
}
339+
return false;
340+
} else if (!stack.isEmpty() && !tokenizer.hasNextToken()) {
341+
currentEvent = currentContext.getNextEvent();
342+
return false;
343+
}
344+
return true;
332345
}
333346

334347
@Override
@@ -427,7 +440,16 @@ private final class ObjectContext extends Context {
427440
public Event getNextEvent() {
428441
// Handle 1. } 2. name:value 3. ,name:value
429442
JsonToken token = tokenizer.nextToken();
430-
if (currentEvent == Event.KEY_NAME) {
443+
if (token == JsonToken.EOF) {
444+
switch (currentEvent) {
445+
case START_OBJECT:
446+
throw parsingException(token, "[STRING, CURLYCLOSE]");
447+
case KEY_NAME:
448+
throw parsingException(token, "[COLON]");
449+
default:
450+
throw parsingException(token, "[COMMA, CURLYCLOSE]");
451+
}
452+
} else if (currentEvent == Event.KEY_NAME) {
431453
// Handle 1. :value
432454
if (token != JsonToken.COLON) {
433455
throw parsingException(token, "[COLON]");
@@ -492,6 +514,14 @@ private final class ArrayContext extends Context {
492514
@Override
493515
public Event getNextEvent() {
494516
JsonToken token = tokenizer.nextToken();
517+
if (token == JsonToken.EOF) {
518+
switch (currentEvent) {
519+
case START_ARRAY:
520+
throw parsingException(token, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL]");
521+
default:
522+
throw parsingException(token, "[COMMA, CURLYCLOSE]");
523+
}
524+
}
495525
if (token == JsonToken.SQUARECLOSE) {
496526
currentContext = stack.pop();
497527
return Event.END_ARRAY;

tests/src/test/java/org/glassfish/json/tests/JsonParserSkipTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at

tests/src/test/java/org/glassfish/json/tests/JsonParserTest.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -36,6 +36,7 @@
3636
import java.util.NoSuchElementException;
3737
import java.util.Random;
3838
import java.util.Scanner;
39+
import javax.json.stream.JsonParsingException;
3940

4041
import org.glassfish.json.api.BufferPool;
4142

@@ -763,4 +764,61 @@ public void testStringUsingBuffers() throws Throwable {
763764
}
764765
}
765766

767+
public void testExceptionsFromHasNext() {
768+
checkExceptionFromHasNext("{");
769+
checkExceptionFromHasNext("{\"key\"");
770+
checkExceptionFromHasNext("{\"name\" : \"prop\"");
771+
checkExceptionFromHasNext("{\"name\" : 3");
772+
checkExceptionFromHasNext("{\"name\" : true");
773+
checkExceptionFromHasNext("{\"name\" : null");
774+
checkExceptionFromHasNext("{\"name\" : {\"$eq\":\"cdc\"}");
775+
checkExceptionFromHasNext("{\"name\" : [{\"$eq\":\"cdc\"}]");
776+
checkExceptionFromHasNext("[");
777+
checkExceptionFromHasNext("{\"name\" : [{\"key\" : [[{\"a\" : 1}]");
778+
checkExceptionFromHasNext("{\"unique\":true,\"name\":\"jUnitTestIndexNeg005\", \"fields\":[{\"order\":-1,\"path\":\"city.zip\"}");
779+
}
780+
781+
public void testEOFFromHasNext() {
782+
checkExceptionFromHasNext("{ \"d\" : 1 } 2 3 4");
783+
}
784+
785+
public void testExceptionsFromNext() {
786+
checkExceptionFromNext("{\"name\" : fal");
787+
checkExceptionFromNext("{\"name\" : nu");
788+
checkExceptionFromNext("{\"name\" : \"pro");
789+
checkExceptionFromNext("{\"key\":");
790+
checkExceptionFromNext("fal");
791+
}
792+
793+
private void checkExceptionFromHasNext(String input) {
794+
try (JsonParser parser = Json.createParser(new StringReader(input))) {
795+
try {
796+
while (parser.hasNext()) {
797+
try {
798+
parser.next();
799+
} catch (Throwable t1) {
800+
fail("Exception should occur from hasNext() for '" + input + "'");
801+
}
802+
}
803+
} catch (JsonParsingException t) {
804+
//this is OK
805+
return;
806+
}
807+
}
808+
fail();
809+
}
810+
811+
private void checkExceptionFromNext(String input) {
812+
try (JsonParser parser = Json.createParser(new StringReader(input))) {
813+
while (parser.hasNext()) {
814+
try {
815+
parser.next();
816+
} catch (JsonParsingException t) {
817+
//this is OK
818+
return;
819+
}
820+
}
821+
}
822+
fail();
823+
}
766824
}

0 commit comments

Comments
 (0)