diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 63effc5f7..e4d4e166b 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -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); } /** @@ -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); } /**