mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Add tests (+ fix bugs) & missing javadoc
This commit is contained in:
parent
6dc1ed0a02
commit
af8cb376c2
@ -30,22 +30,50 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected JSONParserConfiguration clone() {
|
protected JSONParserConfiguration clone() {
|
||||||
return new JSONParserConfiguration();
|
JSONParserConfiguration clone = new JSONParserConfiguration(overwriteDuplicateKey);
|
||||||
|
clone.maxNestingDepth = maxNestingDepth;
|
||||||
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the maximum nesting depth that the parser will descend before throwing an exception
|
||||||
|
* when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into
|
||||||
|
* 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
|
||||||
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) {
|
public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) {
|
||||||
return super.withMaxNestingDepth(maxNestingDepth);
|
JSONParserConfiguration clone = this.clone();
|
||||||
|
clone.maxNestingDepth = maxNestingDepth;
|
||||||
|
|
||||||
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controls the parser's behavior when meeting duplicate keys.
|
||||||
|
* 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.
|
||||||
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
|
*/
|
||||||
public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwriteDuplicateKey) {
|
public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwriteDuplicateKey) {
|
||||||
JSONParserConfiguration newConfig = this.clone();
|
JSONParserConfiguration clone = this.clone();
|
||||||
newConfig.overwriteDuplicateKey = overwriteDuplicateKey;
|
clone.overwriteDuplicateKey = overwriteDuplicateKey;
|
||||||
|
|
||||||
return newConfig;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The parser's behavior when meeting duplicate keys, controls whether the parser should
|
||||||
|
* overwrite duplicate keys or not.
|
||||||
|
*
|
||||||
|
* @return The <code>overwriteDuplicateKey</code> configuration value.
|
||||||
|
*/
|
||||||
public boolean isOverwriteDuplicateKey() {
|
public boolean isOverwriteDuplicateKey() {
|
||||||
return this.overwriteDuplicateKey;
|
return this.overwriteDuplicateKey;
|
||||||
}
|
}
|
||||||
|
@ -20,12 +20,12 @@ public class ParserConfiguration {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies if values should be kept as strings (<code>true</code>), or if
|
* Specifies if values should be kept as strings (<code>true</code>), or if
|
||||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
* they should try to be guessed into JSON values (numeric, boolean, string).
|
||||||
*/
|
*/
|
||||||
protected boolean keepStrings;
|
protected boolean keepStrings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum nesting depth when parsing a document.
|
* The maximum nesting depth when parsing an object.
|
||||||
*/
|
*/
|
||||||
protected int maxNestingDepth;
|
protected int maxNestingDepth;
|
||||||
|
|
||||||
@ -59,14 +59,14 @@ public class ParserConfiguration {
|
|||||||
// map should be cloned as well. If the values of the map are known to also
|
// map should be cloned as well. If the values of the map are known to also
|
||||||
// be immutable, then a shallow clone of the map is acceptable.
|
// be immutable, then a shallow clone of the map is acceptable.
|
||||||
return new ParserConfiguration(
|
return new ParserConfiguration(
|
||||||
this.keepStrings,
|
this.keepStrings,
|
||||||
this.maxNestingDepth
|
this.maxNestingDepth
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
|
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
|
||||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
* they should try to be guessed into JSON values (numeric, boolean, string).
|
||||||
*
|
*
|
||||||
* @return The <code>keepStrings</code> configuration value.
|
* @return The <code>keepStrings</code> configuration value.
|
||||||
*/
|
*/
|
||||||
@ -78,22 +78,21 @@ public class ParserConfiguration {
|
|||||||
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
|
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
|
||||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||||
*
|
*
|
||||||
* @param newVal
|
* @param newVal new value to use for the <code>keepStrings</code> configuration option.
|
||||||
* new value to use for the <code>keepStrings</code> configuration option.
|
* @param <T> the type of the configuration object
|
||||||
* @param <T> the type of the configuration object
|
|
||||||
*
|
|
||||||
* @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.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T extends ParserConfiguration> T withKeepStrings(final boolean newVal) {
|
public <T extends ParserConfiguration> T withKeepStrings(final boolean newVal) {
|
||||||
T newConfig = (T)this.clone();
|
T newConfig = (T) this.clone();
|
||||||
newConfig.keepStrings = newVal;
|
newConfig.keepStrings = newVal;
|
||||||
return newConfig;
|
return newConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum nesting depth that the parser will descend before throwing an exception
|
* The maximum nesting depth that the parser will descend before throwing an exception
|
||||||
* when parsing the XML into JSONML.
|
* when parsing an object (e.g. Map, Collection) into JSON-related objects.
|
||||||
|
*
|
||||||
* @return the maximum nesting depth set for this configuration
|
* @return the maximum nesting depth set for this configuration
|
||||||
*/
|
*/
|
||||||
public int getMaxNestingDepth() {
|
public int getMaxNestingDepth() {
|
||||||
@ -102,18 +101,19 @@ public class ParserConfiguration {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the maximum nesting depth that the parser will descend before throwing an exception
|
* Defines the maximum nesting depth that the parser will descend before throwing an exception
|
||||||
* when parsing the XML into JSONML. The default max nesting depth is 512, which means the parser
|
* when parsing an object (e.g. Map, Collection) into JSON-related objects.
|
||||||
* will throw a JsonException if the maximum depth is reached.
|
* The default max nesting depth is 512, which means the parser will throw a JsonException if
|
||||||
|
* the maximum depth is reached.
|
||||||
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
|
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
|
||||||
* which means the parses will go as deep as the maximum call stack size allows.
|
* which means the parses will go as deep as the maximum call stack size allows.
|
||||||
|
*
|
||||||
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
|
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
|
||||||
* @param <T> the type of the configuration object
|
* @param <T> the type of the configuration object
|
||||||
*
|
|
||||||
* @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.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth) {
|
public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth) {
|
||||||
T newConfig = (T)this.clone();
|
T newConfig = (T) this.clone();
|
||||||
|
|
||||||
if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) {
|
if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) {
|
||||||
newConfig.maxNestingDepth = maxNestingDepth;
|
newConfig.maxNestingDepth = maxNestingDepth;
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
package org.json.junit;
|
|
||||||
|
|
||||||
import org.json.*;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class JSONObjectDuplicateKeyTest {
|
|
||||||
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
|
|
||||||
|
|
||||||
@Test(expected = JSONException.class)
|
|
||||||
public void testThrowException() {
|
|
||||||
new JSONObject(TEST_SOURCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOverwrite() {
|
|
||||||
JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true));
|
|
||||||
|
|
||||||
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,45 @@
|
|||||||
|
package org.json.junit;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.json.JSONParserConfiguration;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class JSONParserConfigurationTest {
|
||||||
|
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
|
||||||
|
|
||||||
|
@Test(expected = JSONException.class)
|
||||||
|
public void testThrowException() {
|
||||||
|
new JSONObject(TEST_SOURCE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOverwrite() {
|
||||||
|
JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true));
|
||||||
|
|
||||||
|
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyDuplicateKeyThenMaxDepth() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withOverwriteDuplicateKey(true)
|
||||||
|
.withMaxNestingDepth(42);
|
||||||
|
|
||||||
|
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
|
||||||
|
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyMaxDepthThenDuplicateKey() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withMaxNestingDepth(42)
|
||||||
|
.withOverwriteDuplicateKey(true);
|
||||||
|
|
||||||
|
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
|
||||||
|
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user