mirror of
https://github.com/fankes/JSON-java-compat.git
synced 2025-09-09 03:54:36 +08:00
Revert some unnecessary changes (mentioned in #840)
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
package org.json;
|
||||
|
||||
/**
|
||||
* An enum class that is supposed to be used in {@link JSONParserConfiguration},
|
||||
* it dedicates which way should be used to handle duplicate keys.
|
||||
*/
|
||||
public enum JSONDuplicateKeyStrategy {
|
||||
/**
|
||||
* The default value. And this is the way it used to be in the previous versions.<br>
|
||||
* The JSONParser will throw an {@link JSONException} when meet duplicate key.
|
||||
*/
|
||||
THROW_EXCEPTION,
|
||||
|
||||
/**
|
||||
* The JSONParser will ignore duplicate keys and won't overwrite the value of the key.
|
||||
*/
|
||||
IGNORE,
|
||||
|
||||
/**
|
||||
* The JSONParser will overwrite the old value of the key.
|
||||
*/
|
||||
OVERWRITE,
|
||||
|
||||
/**
|
||||
* The JSONParser will try to merge the values of the duplicate key into a {@link JSONArray}.
|
||||
*/
|
||||
MERGE_INTO_ARRAY
|
||||
}
|
@@ -212,10 +212,6 @@ public class JSONObject {
|
||||
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 '{'");
|
||||
@@ -243,44 +239,14 @@ public class JSONObject {
|
||||
if (key != null) {
|
||||
// Check if key exists
|
||||
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();
|
||||
if (keyExists && !jsonParserConfiguration.isOverwriteDuplicateKey()) {
|
||||
throw x.syntaxError("Duplicate key \"" + key + "\"");
|
||||
}
|
||||
|
||||
if (keyExists) {
|
||||
switch (duplicateKeyStrategy) {
|
||||
case THROW_EXCEPTION:
|
||||
throw x.syntaxError("Duplicate key \"" + key + "\"");
|
||||
|
||||
case MERGE_INTO_ARRAY:
|
||||
if (mergedKeys == null) {
|
||||
mergedKeys = new ArrayList<String>();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
Object value = x.nextValue();
|
||||
// Only add value if non-null
|
||||
if (value != null) {
|
||||
this.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -5,25 +5,27 @@ package org.json;
|
||||
*/
|
||||
public class JSONParserConfiguration extends ParserConfiguration {
|
||||
/**
|
||||
* The way should be used to handle duplicate keys.
|
||||
* Used to indicate whether to overwrite duplicate key or not.
|
||||
*/
|
||||
private JSONDuplicateKeyStrategy duplicateKeyStrategy;
|
||||
private boolean overwriteDuplicateKey;
|
||||
|
||||
/**
|
||||
* Configuration with the default values.
|
||||
*/
|
||||
public JSONParserConfiguration() {
|
||||
this(JSONDuplicateKeyStrategy.THROW_EXCEPTION);
|
||||
this(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the parser with {@link JSONDuplicateKeyStrategy}.
|
||||
* Configure the parser with argument overwriteDuplicateKey.
|
||||
*
|
||||
* @param duplicateKeyStrategy Indicate which way should be used to handle duplicate keys.
|
||||
* @param overwriteDuplicateKey Indicate whether to overwrite duplicate key or not.<br>
|
||||
* If not, the JSONParser will throw a {@link JSONException}
|
||||
* when meeting duplicate keys.
|
||||
*/
|
||||
public JSONParserConfiguration(JSONDuplicateKeyStrategy duplicateKeyStrategy) {
|
||||
public JSONParserConfiguration(boolean overwriteDuplicateKey) {
|
||||
super();
|
||||
this.duplicateKeyStrategy = duplicateKeyStrategy;
|
||||
this.overwriteDuplicateKey = overwriteDuplicateKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,14 +39,14 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
||||
return super.withMaxNestingDepth(maxNestingDepth);
|
||||
}
|
||||
|
||||
public JSONParserConfiguration withDuplicateKeyStrategy(final JSONDuplicateKeyStrategy duplicateKeyStrategy) {
|
||||
public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwriteDuplicateKey) {
|
||||
JSONParserConfiguration newConfig = this.clone();
|
||||
newConfig.duplicateKeyStrategy = duplicateKeyStrategy;
|
||||
newConfig.overwriteDuplicateKey = overwriteDuplicateKey;
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
public JSONDuplicateKeyStrategy getDuplicateKeyStrategy() {
|
||||
return this.duplicateKeyStrategy;
|
||||
public boolean isOverwriteDuplicateKey() {
|
||||
return this.overwriteDuplicateKey;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user