fix parenthesis matching for JSON validation.
This commit is contained in:
parent
e8a0bba072
commit
29484b2927
|
@ -51,6 +51,8 @@ public class JsonLexer {
|
||||||
private boolean isUnquoted;
|
private boolean isUnquoted;
|
||||||
private String sourceName;
|
private String sourceName;
|
||||||
|
|
||||||
|
private final Stack<String> parenthesis = new Stack<>();
|
||||||
|
|
||||||
public JsonLexer(String source, boolean allowComments, boolean allowUnquotedStrings, int line) throws IOException {
|
public JsonLexer(String source, boolean allowComments, boolean allowUnquotedStrings, int line) throws IOException {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
this.allowComments = allowComments;
|
this.allowComments = allowComments;
|
||||||
|
@ -162,6 +164,7 @@ public class JsonLexer {
|
||||||
return lastLocationAWS;
|
return lastLocationAWS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void next() throws IOException {
|
public void next() throws IOException {
|
||||||
lastLocationBWS = location.copy();
|
lastLocationBWS = location.copy();
|
||||||
char ch;
|
char ch;
|
||||||
|
@ -185,15 +188,25 @@ public class JsonLexer {
|
||||||
} while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
|
} while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
|
||||||
lastLocationAWS = location.copy().prev();
|
lastLocationAWS = location.copy().prev();
|
||||||
isUnquoted = false;
|
isUnquoted = false;
|
||||||
|
|
||||||
if (!more()) {
|
if (!more()) {
|
||||||
type = TokenType.Eof;
|
type = TokenType.Eof;
|
||||||
|
if(!parenthesis.empty()) {
|
||||||
|
throw error("parenthesis matching is not respected. One or more parenthesis were not closed : " + parenthesis);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case '{' :
|
case '{' :
|
||||||
type = TokenType.Open;
|
type = TokenType.Open;
|
||||||
|
parenthesis.push("{");
|
||||||
break;
|
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;
|
type = TokenType.Close;
|
||||||
break;
|
break;
|
||||||
case '"' :
|
case '"' :
|
||||||
|
@ -230,9 +243,17 @@ public class JsonLexer {
|
||||||
type = TokenType.Comma;
|
type = TokenType.Comma;
|
||||||
break;
|
break;
|
||||||
case '[' :
|
case '[' :
|
||||||
|
parenthesis.push("[");
|
||||||
type = TokenType.OpenArray;
|
type = TokenType.OpenArray;
|
||||||
break;
|
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;
|
type = TokenType.CloseArray;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in New Issue