Skip to content

Commit af8cb37

Browse files
committed
Add tests (+ fix bugs) & missing javadoc
1 parent 6dc1ed0 commit af8cb37

File tree

4 files changed

+94
-44
lines changed

4 files changed

+94
-44
lines changed

src/main/java/org/json/JSONParserConfiguration.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,50 @@ public JSONParserConfiguration(boolean overwriteDuplicateKey) {
3030

3131
@Override
3232
protected JSONParserConfiguration clone() {
33-
return new JSONParserConfiguration();
33+
JSONParserConfiguration clone = new JSONParserConfiguration(overwriteDuplicateKey);
34+
clone.maxNestingDepth = maxNestingDepth;
35+
return clone;
3436
}
3537

38+
/**
39+
* Defines the maximum nesting depth that the parser will descend before throwing an exception
40+
* when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into
41+
* JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException
42+
* if the maximum depth is reached.
43+
*
44+
* @param maxNestingDepth the maximum nesting depth allowed to the JSON parser
45+
* @return The existing configuration will not be modified. A new configuration is returned.
46+
*/
3647
@SuppressWarnings("unchecked")
3748
@Override
3849
public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) {
39-
return super.withMaxNestingDepth(maxNestingDepth);
50+
JSONParserConfiguration clone = this.clone();
51+
clone.maxNestingDepth = maxNestingDepth;
52+
53+
return clone;
4054
}
4155

56+
/**
57+
* Controls the parser's behavior when meeting duplicate keys.
58+
* If set to false, the parser will throw a JSONException when meeting a duplicate key.
59+
* Or the duplicate key's value will be overwritten.
60+
*
61+
* @param overwriteDuplicateKey defines should the parser overwrite duplicate keys.
62+
* @return The existing configuration will not be modified. A new configuration is returned.
63+
*/
4264
public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwriteDuplicateKey) {
43-
JSONParserConfiguration newConfig = this.clone();
44-
newConfig.overwriteDuplicateKey = overwriteDuplicateKey;
65+
JSONParserConfiguration clone = this.clone();
66+
clone.overwriteDuplicateKey = overwriteDuplicateKey;
4567

46-
return newConfig;
68+
return clone;
4769
}
4870

71+
/**
72+
* The parser's behavior when meeting duplicate keys, controls whether the parser should
73+
* overwrite duplicate keys or not.
74+
*
75+
* @return The <code>overwriteDuplicateKey</code> configuration value.
76+
*/
4977
public boolean isOverwriteDuplicateKey() {
5078
return this.overwriteDuplicateKey;
5179
}

src/main/java/org/json/ParserConfiguration.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public class ParserConfiguration {
2020

2121
/**
2222
* Specifies if values should be kept as strings (<code>true</code>), or if
23-
* they should try to be guessed into JSON values (numeric, boolean, string)
23+
* they should try to be guessed into JSON values (numeric, boolean, string).
2424
*/
2525
protected boolean keepStrings;
2626

2727
/**
28-
* The maximum nesting depth when parsing a document.
28+
* The maximum nesting depth when parsing an object.
2929
*/
3030
protected int maxNestingDepth;
3131

@@ -59,14 +59,14 @@ protected ParserConfiguration clone() {
5959
// map should be cloned as well. If the values of the map are known to also
6060
// be immutable, then a shallow clone of the map is acceptable.
6161
return new ParserConfiguration(
62-
this.keepStrings,
63-
this.maxNestingDepth
62+
this.keepStrings,
63+
this.maxNestingDepth
6464
);
6565
}
6666

6767
/**
6868
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
69-
* they should try to be guessed into JSON values (numeric, boolean, string)
69+
* they should try to be guessed into JSON values (numeric, boolean, string).
7070
*
7171
* @return The <code>keepStrings</code> configuration value.
7272
*/
@@ -78,22 +78,21 @@ public boolean isKeepStrings() {
7878
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
7979
* they should try to be guessed into JSON values (numeric, boolean, string)
8080
*
81-
* @param newVal
82-
* new value to use for the <code>keepStrings</code> configuration option.
83-
* @param <T> the type of the configuration object
84-
*
81+
* @param newVal new value to use for the <code>keepStrings</code> configuration option.
82+
* @param <T> the type of the configuration object
8583
* @return The existing configuration will not be modified. A new configuration is returned.
8684
*/
8785
@SuppressWarnings("unchecked")
8886
public <T extends ParserConfiguration> T withKeepStrings(final boolean newVal) {
89-
T newConfig = (T)this.clone();
87+
T newConfig = (T) this.clone();
9088
newConfig.keepStrings = newVal;
9189
return newConfig;
9290
}
9391

9492
/**
9593
* The maximum nesting depth that the parser will descend before throwing an exception
96-
* when parsing the XML into JSONML.
94+
* when parsing an object (e.g. Map, Collection) into JSON-related objects.
95+
*
9796
* @return the maximum nesting depth set for this configuration
9897
*/
9998
public int getMaxNestingDepth() {
@@ -102,18 +101,19 @@ public int getMaxNestingDepth() {
102101

103102
/**
104103
* Defines the maximum nesting depth that the parser will descend before throwing an exception
105-
* when parsing the XML into JSONML. The default max nesting depth is 512, which means the parser
106-
* will throw a JsonException if the maximum depth is reached.
104+
* when parsing an object (e.g. Map, Collection) into JSON-related objects.
105+
* The default max nesting depth is 512, which means the parser will throw a JsonException if
106+
* the maximum depth is reached.
107107
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
108108
* which means the parses will go as deep as the maximum call stack size allows.
109+
*
109110
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
110-
* @param <T> the type of the configuration object
111-
*
111+
* @param <T> the type of the configuration object
112112
* @return The existing configuration will not be modified. A new configuration is returned.
113113
*/
114114
@SuppressWarnings("unchecked")
115115
public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth) {
116-
T newConfig = (T)this.clone();
116+
T newConfig = (T) this.clone();
117117

118118
if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) {
119119
newConfig.maxNestingDepth = maxNestingDepth;

src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.json.junit;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
import org.json.JSONParserConfiguration;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertTrue;
10+
11+
public class JSONParserConfigurationTest {
12+
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
13+
14+
@Test(expected = JSONException.class)
15+
public void testThrowException() {
16+
new JSONObject(TEST_SOURCE);
17+
}
18+
19+
@Test
20+
public void testOverwrite() {
21+
JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true));
22+
23+
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
24+
}
25+
26+
@Test
27+
public void verifyDuplicateKeyThenMaxDepth() {
28+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
29+
.withOverwriteDuplicateKey(true)
30+
.withMaxNestingDepth(42);
31+
32+
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
33+
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
34+
}
35+
36+
@Test
37+
public void verifyMaxDepthThenDuplicateKey() {
38+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
39+
.withMaxNestingDepth(42)
40+
.withOverwriteDuplicateKey(true);
41+
42+
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
43+
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
44+
}
45+
}

0 commit comments

Comments
 (0)