Skip to content

Commit

Permalink
moved double quote enforcement to nextString
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkarth committed Sep 10, 2024
1 parent 3f92015 commit 5403a11
Showing 1 changed file with 64 additions and 64 deletions.
128 changes: 64 additions & 64 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,63 +334,71 @@ public char nextClean() throws JSONException {
* @return A String.
* @throws JSONException Unterminated string or unbalanced quotes if strictMode == true.
*/
public String nextString(char quote) throws JSONException {
char c;
StringBuilder sb = new StringBuilder();
for (;;) {
c = this.next();
switch (c) {
case 0:
case '\n':
case '\r':
throw this.syntaxError("Unterminated string. " +
"Character with int code " + (int) c + " is not allowed within a quoted string.");
case '\\':
c = this.next();
switch (c) {
case 'b':
sb.append('\b');
break;
case 't':
sb.append('\t');
break;
case 'n':
sb.append('\n');
break;
case 'f':
sb.append('\f');
break;
case 'r':
sb.append('\r');
break;
case 'u':
String next = this.next(4);
try {
sb.append((char) Integer.parseInt(next, 16));
} catch (NumberFormatException e) {
throw this.syntaxError("Illegal escape. " +
"\\u must be followed by a 4 digit hexadecimal number. \\" + next
+ " is not valid.",
e);
}
break;
case '"':
case '\'':
case '\\':
case '/':
sb.append(c);
break;
default:
throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
}
break;
default:
if (c == quote) {
return sb.toString();
}
sb.append(c);
public Object nextString(char quote, boolean strictMode) throws JSONException {
if (strictMode && quote == '\'') {
throw this.syntaxError("Single quote wrap not allowed in strict mode");
}

if (quote == '"' || quote == '\'') {
char c;
StringBuilder sb = new StringBuilder();
for (; ; ) {
c = this.next();
switch (c) {
case 0:
case '\n':
case '\r':
throw this.syntaxError("Unterminated string. " +
"Character with int code " + (int) c + " is not allowed within a quoted string.");
case '\\':
c = this.next();
switch (c) {
case 'b':
sb.append('\b');
break;
case 't':
sb.append('\t');
break;
case 'n':
sb.append('\n');
break;
case 'f':
sb.append('\f');
break;
case 'r':
sb.append('\r');
break;
case 'u':
String next = this.next(4);
try {
sb.append((char) Integer.parseInt(next, 16));
} catch (NumberFormatException e) {
throw this.syntaxError("Illegal escape. " +
"\\u must be followed by a 4 digit hexadecimal number. \\" + next
+ " is not valid.",
e);
}
break;
case '"':
case '\'':
case '\\':
case '/':
sb.append(c);
break;
default:
throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
}
break;
default:
if (c == quote) {
return sb.toString();
}
sb.append(c);
}
}
}

return parsedUnquotedText(quote, strictMode);
}

/**
Expand Down Expand Up @@ -528,15 +536,7 @@ private JSONArray getJsonArray() {
Object nextSimpleValue(char c, JSONParserConfiguration jsonParserConfiguration) {
boolean strictMode = jsonParserConfiguration.isStrictMode();

if (strictMode && c == '\'') {
throw this.syntaxError("Single quote wrap not allowed in strict mode");
}

if (c == '"' || c == '\'') {
return this.nextString(c);
}

return parsedUnquotedText(c, strictMode);
return this.nextString(c, strictMode);
}

/**
Expand Down

0 comments on commit 5403a11

Please sign in to comment.