chore(#871-strictMode): reverted unrelated changes

This commit is contained in:
rikkarth 2024-03-17 15:20:38 +00:00
parent f3b3491f4d
commit 3672b5e158
No known key found for this signature in database
GPG Key ID: 11E5F28B0AED6AC7
5 changed files with 1712 additions and 1190 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,6 @@ package org.json;
* Configuration object for the JSON parser. The configuration is immutable. * Configuration object for the JSON parser. The configuration is immutable.
*/ */
public class JSONParserConfiguration extends ParserConfiguration { public class JSONParserConfiguration extends ParserConfiguration {
/** /**
* Used to indicate whether to overwrite duplicate key or not. * Used to indicate whether to overwrite duplicate key or not.
*/ */
@ -34,9 +33,10 @@ public class JSONParserConfiguration extends ParserConfiguration {
} }
/** /**
* Defines the maximum nesting depth that the parser will descend before throwing an exception when parsing a map * Defines the maximum nesting depth that the parser will descend before throwing an exception
* into JSONObject or parsing a {@link java.util.Collection} instance into JSONArray. The default max nesting depth * when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into
* is 512, which means the parser will throw a JsonException if the maximum depth is reached. * JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException
* if the maximum depth is reached.
* *
* @param maxNestingDepth the maximum nesting depth allowed to the JSON parser * @param maxNestingDepth the maximum nesting depth allowed to the JSON parser
* @return The existing configuration will not be modified. A new configuration is returned. * @return The existing configuration will not be modified. A new configuration is returned.
@ -51,8 +51,9 @@ public class JSONParserConfiguration extends ParserConfiguration {
} }
/** /**
* Controls the parser's behavior when meeting duplicate keys. If set to false, the parser will throw a * Controls the parser's behavior when meeting duplicate keys.
* JSONException when meeting a duplicate key. Or the duplicate key's value will be overwritten. * If set to false, the parser will throw a JSONException when meeting a duplicate key.
* Or the duplicate key's value will be overwritten.
* *
* @param overwriteDuplicateKey defines should the parser overwrite duplicate keys. * @param overwriteDuplicateKey defines should the parser overwrite duplicate keys.
* @return The existing configuration will not be modified. A new configuration is returned. * @return The existing configuration will not be modified. A new configuration is returned.
@ -83,8 +84,8 @@ public class JSONParserConfiguration extends ParserConfiguration {
} }
/** /**
* The parser's behavior when meeting duplicate keys, controls whether the parser should overwrite duplicate keys or * The parser's behavior when meeting duplicate keys, controls whether the parser should
* not. * overwrite duplicate keys or not.
* *
* @return The <code>overwriteDuplicateKey</code> configuration value. * @return The <code>overwriteDuplicateKey</code> configuration value.
*/ */

View File

@ -8,57 +8,40 @@ Public Domain.
*/ */
/** /**
* A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and * A JSONTokener takes a source string and extracts characters and tokens from
* JSONArray constructors to parse JSON source strings. * it. It is used by the JSONObject and JSONArray constructors to parse
* * JSON source strings.
* @author JSON.org * @author JSON.org
* @version 2014-05-03 * @version 2014-05-03
*/ */
public class JSONTokener { public class JSONTokener {
/** current read character position on the current line. */
/**
* current read character position on the current line.
*/
private long character; private long character;
/** /** flag to indicate if the end of the input has been found. */
* flag to indicate if the end of the input has been found.
*/
private boolean eof; private boolean eof;
/** /** current read index of the input. */
* current read index of the input.
*/
private long index; private long index;
/** /** current line of the input. */
* current line of the input.
*/
private long line; private long line;
/** /** previous character read from the input. */
* previous character read from the input.
*/
private char previous; private char previous;
/** /** Reader for the input. */
* Reader for the input.
*/
private final Reader reader; private final Reader reader;
/** /** flag to indicate that a previous character was requested. */
* flag to indicate that a previous character was requested.
*/
private boolean usePrevious; private boolean usePrevious;
/** /** the number of characters read in the previous line. */
* the number of characters read in the previous line.
*/
private long characterPreviousLine; private long characterPreviousLine;
/** /**
* Construct a JSONTokener from a Reader. The caller must close the Reader. * Construct a JSONTokener from a Reader. The caller must close the Reader.
* *
* @param reader A reader. * @param reader A reader.
*/ */
public JSONTokener(Reader reader) { public JSONTokener(Reader reader) {
this.reader = reader.markSupported() this.reader = reader.markSupported()
? reader ? reader
: new BufferedReader(reader); : new BufferedReader(reader);
this.eof = false; this.eof = false;
this.usePrevious = false; this.usePrevious = false;
this.previous = 0; this.previous = 0;
@ -71,7 +54,6 @@ public class JSONTokener {
/** /**
* Construct a JSONTokener from an InputStream. The caller must close the input stream. * Construct a JSONTokener from an InputStream. The caller must close the input stream.
*
* @param inputStream The source. * @param inputStream The source.
*/ */
public JSONTokener(InputStream inputStream) { public JSONTokener(InputStream inputStream) {
@ -82,7 +64,7 @@ public class JSONTokener {
/** /**
* Construct a JSONTokener from a string. * Construct a JSONTokener from a string.
* *
* @param s A source string. * @param s A source string.
*/ */
public JSONTokener(String s) { public JSONTokener(String s) {
this(new StringReader(s)); this(new StringReader(s));
@ -90,10 +72,11 @@ public class JSONTokener {
/** /**
* Back up one character. This provides a sort of lookahead capability, so that you can test for a digit or letter * Back up one character. This provides a sort of lookahead capability,
* before attempting to parse the next number or identifier. * so that you can test for a digit or letter before attempting to parse
* * the next number or identifier.
* @throws JSONException Thrown if trying to step back more than 1 step or if already at the start of the string * @throws JSONException Thrown if trying to step back more than 1 step
* or if already at the start of the string
*/ */
public void back() throws JSONException { public void back() throws JSONException {
if (this.usePrevious || this.index <= 0) { if (this.usePrevious || this.index <= 0) {
@ -109,19 +92,19 @@ public class JSONTokener {
*/ */
private void decrementIndexes() { private void decrementIndexes() {
this.index--; this.index--;
if (this.previous == '\r' || this.previous == '\n') { if(this.previous=='\r' || this.previous == '\n') {
this.line--; this.line--;
this.character = this.characterPreviousLine; this.character=this.characterPreviousLine ;
} else if (this.character > 0) { } else if(this.character > 0){
this.character--; this.character--;
} }
} }
/** /**
* Get the hex value of a character (base16). * Get the hex value of a character (base16).
* * @param c A character between '0' and '9' or between 'A' and 'F' or
* @param c A character between '0' and '9' or between 'A' and 'F' or between 'a' and 'f'. * between 'a' and 'f'.
* @return An int between 0 and 15, or -1 if c was not a hex digit. * @return An int between 0 and 15, or -1 if c was not a hex digit.
*/ */
public static int dehexchar(char c) { public static int dehexchar(char c) {
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
@ -147,13 +130,14 @@ public class JSONTokener {
/** /**
* Determine if the source string still contains characters that next() can consume. * Determine if the source string still contains characters that next()
* * can consume.
* @return true if not yet at the end of the source. * @return true if not yet at the end of the source.
* @throws JSONException thrown if there is an error stepping forward or backward while checking for more data. * @throws JSONException thrown if there is an error stepping forward
* or backward while checking for more data.
*/ */
public boolean more() throws JSONException { public boolean more() throws JSONException {
if (this.usePrevious) { if(this.usePrevious) {
return true; return true;
} }
try { try {
@ -163,7 +147,7 @@ public class JSONTokener {
} }
try { try {
// -1 is EOF, but next() can not consume the null character '\0' // -1 is EOF, but next() can not consume the null character '\0'
if (this.reader.read() <= 0) { if(this.reader.read() <= 0) {
this.eof = true; this.eof = true;
return false; return false;
} }
@ -204,32 +188,28 @@ public class JSONTokener {
/** /**
* Get the last character read from the input or '\0' if nothing has been read yet. * Get the last character read from the input or '\0' if nothing has been read yet.
*
* @return the last character read from the input. * @return the last character read from the input.
*/ */
protected char getPrevious() { protected char getPrevious() { return this.previous;}
return this.previous;
}
/** /**
* Increments the internal indexes according to the previous character read and the character passed as the current * Increments the internal indexes according to the previous character
* character. * read and the character passed as the current character.
*
* @param c the current character read. * @param c the current character read.
*/ */
private void incrementIndexes(int c) { private void incrementIndexes(int c) {
if (c > 0) { if(c > 0) {
this.index++; this.index++;
if (c == '\r') { if(c=='\r') {
this.line++; this.line++;
this.characterPreviousLine = this.character; this.characterPreviousLine = this.character;
this.character = 0; this.character=0;
} else if (c == '\n') { }else if (c=='\n') {
if (this.previous != '\r') { if(this.previous != '\r') {
this.line++; this.line++;
this.characterPreviousLine = this.character; this.characterPreviousLine = this.character;
} }
this.character = 0; this.character=0;
} else { } else {
this.character++; this.character++;
} }
@ -237,8 +217,8 @@ public class JSONTokener {
} }
/** /**
* Consume the next character, and check that it matches a specified character. * Consume the next character, and check that it matches a specified
* * character.
* @param c The character to match. * @param c The character to match.
* @return The character. * @return The character.
* @throws JSONException if the character does not match. * @throws JSONException if the character does not match.
@ -246,9 +226,9 @@ public class JSONTokener {
public char next(char c) throws JSONException { public char next(char c) throws JSONException {
char n = this.next(); char n = this.next();
if (n != c) { if (n != c) {
if (n > 0) { if(n > 0) {
throw this.syntaxError("Expected '" + c + "' and instead saw '" + throw this.syntaxError("Expected '" + c + "' and instead saw '" +
n + "'"); n + "'");
} }
throw this.syntaxError("Expected '" + c + "' and instead saw ''"); throw this.syntaxError("Expected '" + c + "' and instead saw ''");
} }
@ -259,9 +239,11 @@ public class JSONTokener {
/** /**
* Get the next n characters. * Get the next n characters.
* *
* @param n The number of characters to take. * @param n The number of characters to take.
* @return A string of n characters. * @return A string of n characters.
* @throws JSONException Substring bounds error if there are not n characters remaining in the source string. * @throws JSONException
* Substring bounds error if there are not
* n characters remaining in the source string.
*/ */
public String next(int n) throws JSONException { public String next(int n) throws JSONException {
if (n == 0) { if (n == 0) {
@ -284,12 +266,11 @@ public class JSONTokener {
/** /**
* Get the next char in the string, skipping whitespace. * Get the next char in the string, skipping whitespace.
*
* @return A character, or 0 if there are no more characters.
* @throws JSONException Thrown if there is an error reading the source string. * @throws JSONException Thrown if there is an error reading the source string.
* @return A character, or 0 if there are no more characters.
*/ */
public char nextClean() throws JSONException { public char nextClean() throws JSONException {
for (; ; ) { for (;;) {
char c = this.next(); char c = this.next();
if (c == 0 || c > ' ') { if (c == 0 || c > ' ') {
return c; return c;
@ -299,13 +280,14 @@ public class JSONTokener {
/** /**
* Return the characters up to the next close quote character. Backslash processing is done. The formal JSON format * Return the characters up to the next close quote character.
* does not allow strings in single quotes, but an implementation is allowed to accept them. * Backslash processing is done. The formal JSON format does not
* * allow strings in single quotes, but an implementation is allowed to
* accept them.
* @param quote The quoting character, either * @param quote The quoting character, either
* <code>"</code>&nbsp;<small>(double quote)</small> or * <code>"</code>&nbsp;<small>(double quote)</small> or
* <code>'</code>&nbsp;<small>(single quote)</small>. * <code>'</code>&nbsp;<small>(single quote)</small>.
* @return A String. * @return A String.
* @throws JSONException Unterminated string. * @throws JSONException Unterminated string.
*/ */
public String nextString(char quote) throws JSONException { public String nextString(char quote) throws JSONException {
@ -314,65 +296,66 @@ public class JSONTokener {
for (;;) { for (;;) {
c = this.next(); c = this.next();
switch (c) { switch (c) {
case 0: case 0:
case '\n': case '\n':
case '\r': case '\r':
throw this.syntaxError("Unterminated string"); throw this.syntaxError("Unterminated string");
case '\\': case '\\':
c = this.next(); c = this.next();
switch (c) { switch (c) {
case 'b': case 'b':
sb.append('\b'); sb.append('\b');
break; break;
case 't': case 't':
sb.append('\t'); sb.append('\t');
break; break;
case 'n': case 'n':
sb.append('\n'); sb.append('\n');
break; break;
case 'f': case 'f':
sb.append('\f'); sb.append('\f');
break; break;
case 'r': case 'r':
sb.append('\r'); sb.append('\r');
break; break;
case 'u': case 'u':
try { try {
sb.append((char) Integer.parseInt(this.next(4), 16)); sb.append((char)Integer.parseInt(this.next(4), 16));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw this.syntaxError("Illegal escape.", e); throw this.syntaxError("Illegal escape.", e);
}
break;
case '"':
case '\'':
case '\\':
case '/':
sb.append(c);
break;
default:
throw this.syntaxError("Illegal escape.");
} }
break; break;
default: case '"':
if (c == quote) { case '\'':
return sb.toString(); case '\\':
} case '/':
sb.append(c); sb.append(c);
break;
default:
throw this.syntaxError("Illegal escape.");
}
break;
default:
if (c == quote) {
return sb.toString();
}
sb.append(c);
} }
} }
} }
/** /**
* Get the text up but not including the specified character or the end of line, whichever comes first. * Get the text up but not including the specified character or the
* * end of line, whichever comes first.
* @param delimiter A delimiter character. * @param delimiter A delimiter character.
* @return A string. * @return A string.
* @throws JSONException Thrown if there is an error while searching for the delimiter * @throws JSONException Thrown if there is an error while searching
* for the delimiter
*/ */
public String nextTo(char delimiter) throws JSONException { public String nextTo(char delimiter) throws JSONException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (; ; ) { for (;;) {
char c = this.next(); char c = this.next();
if (c == delimiter || c == 0 || c == '\n' || c == '\r') { if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
if (c != 0) { if (c != 0) {
@ -386,20 +369,20 @@ public class JSONTokener {
/** /**
* Get the text up but not including one of the specified delimiter characters or the end of line, whichever comes * Get the text up but not including one of the specified delimiter
* first. * characters or the end of line, whichever comes first.
*
* @param delimiters A set of delimiter characters. * @param delimiters A set of delimiter characters.
* @return A string, trimmed. * @return A string, trimmed.
* @throws JSONException Thrown if there is an error while searching for the delimiter * @throws JSONException Thrown if there is an error while searching
* for the delimiter
*/ */
public String nextTo(String delimiters) throws JSONException { public String nextTo(String delimiters) throws JSONException {
char c; char c;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (; ; ) { for (;;) {
c = this.next(); c = this.next();
if (delimiters.indexOf(c) >= 0 || c == 0 || if (delimiters.indexOf(c) >= 0 || c == 0 ||
c == '\n' || c == '\r') { c == '\n' || c == '\r') {
if (c != 0) { if (c != 0) {
this.back(); this.back();
} }
@ -542,8 +525,6 @@ public class JSONTokener {
} }
Object nextSimpleValue(char c, boolean strictMode) { Object nextSimpleValue(char c, boolean strictMode) {
String string;
if (c == '"' || c == '\'') { if (c == '"' || c == '\'') {
String str = this.nextString(c); String str = this.nextString(c);
if (strictMode) { if (strictMode) {
@ -552,6 +533,19 @@ public class JSONTokener {
return str; return str;
} }
return parsedUnquotedText(c);
}
/**
* Parses unquoted text from the JSON input. This could be the values true, false, or null, or it can be a number.
* Non-standard forms are also accepted. Characters are accumulated until the end of the text or a formatting
* character is reached.
*
* @param c The starting character.
* @return The parsed object.
* @throws JSONException If the parsed string is empty.
*/
private Object parsedUnquotedText(char c) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) { while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
sb.append(c); sb.append(c);
@ -561,8 +555,8 @@ public class JSONTokener {
this.back(); this.back();
} }
string = sb.toString().trim(); String string = sb.toString().trim();
if ("".equals(string)) { if (string.isEmpty()) {
throw this.syntaxError("Missing value"); throw this.syntaxError("Missing value");
} }
return JSONObject.stringToValue(string); return JSONObject.stringToValue(string);
@ -570,12 +564,13 @@ public class JSONTokener {
/** /**
* Skip characters until the next character is the requested character. If the requested character is not found, no * Skip characters until the next character is the requested character.
* characters are skipped. * If the requested character is not found, no characters are skipped.
*
* @param to A character to skip to. * @param to A character to skip to.
* @return The requested character, or zero if the requested character is not found. * @return The requested character, or zero if the requested character
* @throws JSONException Thrown if there is an error while searching for the to character * is not found.
* @throws JSONException Thrown if there is an error while searching
* for the to character
*/ */
public char skipTo(char to) throws JSONException { public char skipTo(char to) throws JSONException {
char c; char c;
@ -609,7 +604,7 @@ public class JSONTokener {
* Make a JSONException to signal a syntax error. * Make a JSONException to signal a syntax error.
* *
* @param message The error message. * @param message The error message.
* @return A JSONException object, suitable for throwing * @return A JSONException object, suitable for throwing
*/ */
public JSONException syntaxError(String message) { public JSONException syntaxError(String message) {
return new JSONException(message + this.toString()); return new JSONException(message + this.toString());
@ -618,9 +613,9 @@ public class JSONTokener {
/** /**
* Make a JSONException to signal a syntax error. * Make a JSONException to signal a syntax error.
* *
* @param message The error message. * @param message The error message.
* @param causedBy The throwable that caused the error. * @param causedBy The throwable that caused the error.
* @return A JSONException object, suitable for throwing * @return A JSONException object, suitable for throwing
*/ */
public JSONException syntaxError(String message, Throwable causedBy) { public JSONException syntaxError(String message, Throwable causedBy) {
return new JSONException(message + this.toString(), causedBy); return new JSONException(message + this.toString(), causedBy);
@ -634,7 +629,7 @@ public class JSONTokener {
@Override @Override
public String toString() { public String toString() {
return " at " + this.index + " [character " + this.character + " line " + return " at " + this.index + " [character " + this.character + " line " +
this.line + "]"; this.line + "]";
} }
/** /**
@ -643,7 +638,7 @@ public class JSONTokener {
* @throws IOException If an I/O error occurs while closing the reader. * @throws IOException If an I/O error occurs while closing the reader.
*/ */
public void close() throws IOException { public void close() throws IOException {
if (reader != null) { if(reader!=null){
reader.close(); reader.close();
} }
} }

View File

@ -2,7 +2,6 @@ package org.json.junit;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -14,7 +13,6 @@ import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class JSONParserConfigurationTest { public class JSONParserConfigurationTest {
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}"; private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
@Test(expected = JSONException.class) @Test(expected = JSONException.class)
@ -25,7 +23,7 @@ public class JSONParserConfigurationTest {
@Test @Test
public void testOverwrite() { public void testOverwrite() {
JSONObject jsonObject = new JSONObject(TEST_SOURCE, JSONObject jsonObject = new JSONObject(TEST_SOURCE,
new JSONParserConfiguration().withOverwriteDuplicateKey(true)); new JSONParserConfiguration().withOverwriteDuplicateKey(true));
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
} }
@ -53,8 +51,8 @@ public class JSONParserConfigurationTest {
@Test @Test
public void verifyDuplicateKeyThenMaxDepth() { public void verifyDuplicateKeyThenMaxDepth() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withOverwriteDuplicateKey(true) .withOverwriteDuplicateKey(true)
.withMaxNestingDepth(42); .withMaxNestingDepth(42);
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth()); assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey()); assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
@ -63,8 +61,8 @@ public class JSONParserConfigurationTest {
@Test @Test
public void verifyMaxDepthThenDuplicateKey() { public void verifyMaxDepthThenDuplicateKey() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withMaxNestingDepth(42) .withMaxNestingDepth(42)
.withOverwriteDuplicateKey(true); .withOverwriteDuplicateKey(true);
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey()); assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth()); assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());