Skip to content

Commit 29484b2

Browse files
committed
fix parenthesis matching for JSON validation.
1 parent e8a0bba commit 29484b2

File tree

1 file changed

+25
-4
lines changed
  • org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser

1 file changed

+25
-4
lines changed

org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonLexer.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ boolean isValueType() {
5151
private boolean isUnquoted;
5252
private String sourceName;
5353

54+
private final Stack<String> parenthesis = new Stack<>();
55+
5456
public JsonLexer(String source, boolean allowComments, boolean allowUnquotedStrings, int line) throws IOException {
5557
this.source = source;
5658
this.allowComments = allowComments;
@@ -162,6 +164,7 @@ public JsonLocationData getLastLocationAWS() {
162164
return lastLocationAWS;
163165
}
164166

167+
165168
public void next() throws IOException {
166169
lastLocationBWS = location.copy();
167170
char ch;
@@ -185,15 +188,25 @@ public void next() throws IOException {
185188
} while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
186189
lastLocationAWS = location.copy().prev();
187190
isUnquoted = false;
188-
189191
if (!more()) {
190192
type = TokenType.Eof;
193+
if(!parenthesis.empty()) {
194+
throw error("parenthesis matching is not respected. One or more parenthesis were not closed : " + parenthesis);
195+
}
191196
} else {
192197
switch (ch) {
193198
case '{' :
194199
type = TokenType.Open;
200+
parenthesis.push("{");
195201
break;
196-
case '}' :
202+
case '}' :
203+
if(parenthesis.empty()) {
204+
throw error("Unexpected close marker '}'. No '{' before.");
205+
}
206+
String par = parenthesis.pop();
207+
if(!par.equals("{")) {
208+
throw error("Unexpected close marker '}'. Expected ']'.");
209+
}
197210
type = TokenType.Close;
198211
break;
199212
case '"' :
@@ -229,10 +242,18 @@ public void next() throws IOException {
229242
case ',' :
230243
type = TokenType.Comma;
231244
break;
232-
case '[' :
245+
case '[' :
246+
parenthesis.push("[");
233247
type = TokenType.OpenArray;
234248
break;
235-
case ']' :
249+
case ']' :
250+
if(parenthesis.empty()) {
251+
throw error("Unexpected close marker ']'. No '[' before.");
252+
}
253+
par = parenthesis.pop();
254+
if(!par.equals("[")) {
255+
throw error("Unexpected close marker ']'. Expected '}'");
256+
}
236257
type = TokenType.CloseArray;
237258
break;
238259
default:

0 commit comments

Comments
 (0)