diff --git a/src/main/java/org/json/JSONDuplicateKeyStrategy.java b/src/main/java/org/json/JSONDuplicateKeyStrategy.java deleted file mode 100644 index 954ac3a..0000000 --- a/src/main/java/org/json/JSONDuplicateKeyStrategy.java +++ /dev/null @@ -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.
- * 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 -} diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 1572a81..317fd3d 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -212,10 +212,6 @@ public class JSONObject { this(); char c; String key; - JSONDuplicateKeyStrategy duplicateKeyStrategy = jsonParserConfiguration.getDuplicateKeyStrategy(); - - // A list to store merged keys - List 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(); - } - - 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); } } diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index f1ea2b2..0d8706c 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -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.
+ * 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; } } diff --git a/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java b/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java index 73dc70b..1a3525b 100644 --- a/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java +++ b/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java @@ -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")); } }