mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-02 19:15:27 -04:00
Revert some unnecessary changes (mentioned in #840)
This commit is contained in:
parent
21a9fae7b0
commit
cb2c8d3962
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -7,41 +7,17 @@ import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JSONObjectDuplicateKeyTest {
|
||||
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\", \"key\": \"value3\"}";
|
||||
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
|
||||
|
||||
@Test(expected = JSONException.class)
|
||||
public void testThrowException() {
|
||||
new JSONObject(TEST_SOURCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnore() {
|
||||
JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(
|
||||
JSONDuplicateKeyStrategy.IGNORE
|
||||
));
|
||||
|
||||
assertEquals("duplicate key shouldn't be overwritten", "value1", jsonObject.getString("key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverwrite() {
|
||||
JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(
|
||||
JSONDuplicateKeyStrategy.OVERWRITE
|
||||
));
|
||||
JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true));
|
||||
|
||||
assertEquals("duplicate key should be overwritten", "value3", jsonObject.getString("key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeIntoArray() {
|
||||
JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(
|
||||
JSONDuplicateKeyStrategy.MERGE_INTO_ARRAY
|
||||
));
|
||||
|
||||
JSONArray jsonArray;
|
||||
assertTrue("duplicate key should be merged into JSONArray", jsonObject.get("key") instanceof JSONArray
|
||||
&& (jsonArray = jsonObject.getJSONArray("key")).length() == 3
|
||||
&& jsonArray.getString(0).equals("value1") && jsonArray.getString(1).equals("value2")
|
||||
&& jsonArray.getString(2).equals("value3"));
|
||||
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user