Implemented custom duplicate key handling

- Supports: throw an exception (by default), ignore, overwrite & merge into a JSONArray
 - With tests, 4/4 passed.
This commit is contained in:
XIAYM-gh
2024-02-13 18:56:10 +08:00
parent 010e83b925
commit 10514e48cb
4 changed files with 191 additions and 34 deletions

View File

@@ -15,17 +15,8 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.regex.Pattern;
import static org.json.NumberConversionUtil.potentialNumber;
@@ -203,9 +194,28 @@ public class JSONObject {
* duplicated key.
*/
public JSONObject(JSONTokener x) throws JSONException {
this(x, new JSONParserConfiguration());
}
/**
* Construct a JSONObject from a JSONTokener with custom json parse configurations.
*
* @param x
* A JSONTokener object containing the source string.
* @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this();
char c;
String key;
JSONDuplicateKeyStrategy duplicateKeyStrategy = jsonParserConfiguration.getDuplicateKeyStrategy();
// A list to store merged keys
List<String> mergedKeys = null;
if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'");
@@ -232,14 +242,45 @@ public class JSONObject {
if (key != null) {
// Check if key exists
if (this.opt(key) != null) {
// key already exists
throw x.syntaxError("Duplicate key \"" + key + "\"");
boolean keyExists = this.opt(key) != null;
// Read value early to make the tokener work well
Object value = null;
if (!keyExists || duplicateKeyStrategy != JSONDuplicateKeyStrategy.THROW_EXCEPTION) {
value = x.nextValue();
}
// Only add value if non-null
Object value = x.nextValue();
if (value!=null) {
this.put(key, value);
if (keyExists) {
switch (duplicateKeyStrategy) {
case THROW_EXCEPTION:
throw x.syntaxError("Duplicate key \"" + key + "\"");
case MERGE_INTO_ARRAY:
if (mergedKeys == null) {
mergedKeys = new ArrayList<>();
}
Object current = this.get(key);
if (current instanceof JSONArray && mergedKeys.contains(key)) {
((JSONArray) current).put(value);
break;
}
JSONArray merged = new JSONArray();
merged.put(current);
merged.put(value);
this.put(key, merged);
mergedKeys.add(key);
break;
}
// == IGNORE, ignored :)
}
if (!keyExists || duplicateKeyStrategy == JSONDuplicateKeyStrategy.OVERWRITE) {
// Only add value if non-null
if (value != null) {
this.put(key, value);
}
}
}
@@ -294,7 +335,6 @@ public class JSONObject {
/**
* Construct a JSONObject from a map with recursion depth.
*
*/
private JSONObject(Map<?, ?> m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) {
@@ -426,7 +466,25 @@ public class JSONObject {
* duplicated key.
*/
public JSONObject(String source) throws JSONException {
this(new JSONTokener(source));
this(source, new JSONParserConfiguration());
}
/**
* Construct a JSONObject from a source JSON text string with custom json parse configurations.
* This is the most commonly used JSONObject constructor.
*
* @param source
* A string beginning with <code>{</code>&nbsp;<small>(left
* brace)</small> and ending with <code>}</code>
* &nbsp;<small>(right brace)</small>.
* @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing.
* @exception JSONException
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this(new JSONTokener(source), jsonParserConfiguration);
}
/**