Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix parenthesis matching for JSON validation. #1763

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ boolean isValueType() {
private boolean isUnquoted;
private String sourceName;

private final Stack<String> parenthesis = new Stack<>();

public JsonLexer(String source, boolean allowComments, boolean allowUnquotedStrings, int line) throws IOException {
this.source = source;
this.allowComments = allowComments;
Expand Down Expand Up @@ -162,6 +164,7 @@ public JsonLocationData getLastLocationAWS() {
return lastLocationAWS;
}


public void next() throws IOException {
lastLocationBWS = location.copy();
char ch;
Expand All @@ -185,15 +188,25 @@ public void next() throws IOException {
} while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
lastLocationAWS = location.copy().prev();
isUnquoted = false;

if (!more()) {
type = TokenType.Eof;
if(!parenthesis.empty()) {
throw error("parenthesis matching is not respected. One or more parenthesis were not closed : " + parenthesis);
}
} else {
switch (ch) {
case '{' :
type = TokenType.Open;
parenthesis.push("{");
break;
case '}' :
case '}' :
if(parenthesis.empty()) {
throw error("Unexpected close marker '}'. No '{' before.");
}
String par = parenthesis.pop();
if(!par.equals("{")) {
throw error("Unexpected close marker '}'. Expected ']'.");
}
type = TokenType.Close;
break;
case '"' :
Expand Down Expand Up @@ -229,10 +242,18 @@ public void next() throws IOException {
case ',' :
type = TokenType.Comma;
break;
case '[' :
case '[' :
parenthesis.push("[");
type = TokenType.OpenArray;
break;
case ']' :
case ']' :
if(parenthesis.empty()) {
throw error("Unexpected close marker ']'. No '[' before.");
}
par = parenthesis.pop();
if(!par.equals("[")) {
throw error("Unexpected close marker ']'. Expected '}'");
}
type = TokenType.CloseArray;
break;
default:
Expand Down
Loading