Skip to content

Commit 1afd7cd

Browse files
author
Robert Lichtenberger
committed
Use better name for parser configuration option, fix API comment.
1 parent 5d1c789 commit 1afd7cd

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

src/main/java/org/json/JSONObject.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ private JSONObject(Map<?, ?> m, int recursionDepth, JSONParserConfiguration json
332332
throw new NullPointerException("Null key.");
333333
}
334334
final Object value = e.getValue();
335-
if (value != null || jsonParserConfiguration.isJavaNullAsJsonNull()) {
335+
if (value != null || jsonParserConfiguration.isUseNativeNulls()) {
336336
testValidity(value);
337337
this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration));
338338
}

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class JSONParserConfiguration extends ParserConfiguration {
1010
private boolean overwriteDuplicateKey;
1111

1212
/**
13-
* Used to indicate whether ignore null values when converting java maps to JSONObject or not.
13+
* Used to indicate whether to convert java null values to JSONObject.NULL or ignoring the entry when converting java maps.
1414
*/
15-
private boolean javaNullAsJsonNull;
15+
private boolean useNativeNulls;
1616

1717
/**
1818
* Configuration with the default values.
@@ -74,16 +74,16 @@ public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwrite
7474
}
7575

7676
/**
77-
* Controls the parser's behavior when meeting duplicate keys.
78-
* If set to false, the parser will throw a JSONException when meeting a duplicate key.
79-
* Or the duplicate key's value will be overwritten.
77+
* Controls the parser's behavior when meeting Java null values while converting maps.
78+
* If set to true, the parser will put a JSONObject.NULL into the resulting JSONObject.
79+
* Or the map entry will be ignored.
8080
*
81-
* @param javaNullAsJsonNull define, if the parser should ignore null values in Java maps
81+
* @param useNativeNulls defines if the parser should convert null values in Java maps
8282
* @return The existing configuration will not be modified. A new configuration is returned.
8383
*/
84-
public JSONParserConfiguration withJavaNullAsJsonNull(final boolean javaNullAsJsonNull) {
84+
public JSONParserConfiguration withUseNativeNulls(final boolean useNativeNulls) {
8585
JSONParserConfiguration clone = this.clone();
86-
clone.javaNullAsJsonNull = javaNullAsJsonNull;
86+
clone.useNativeNulls = useNativeNulls;
8787

8888
return clone;
8989
}
@@ -128,13 +128,14 @@ public boolean isOverwriteDuplicateKey() {
128128
}
129129

130130
/**
131-
* The parser's behavior when meeting a null value in a java map, controls whether the parser should ignore
132-
* that map entry or write a JSON entry with a null value.
131+
* The parser's behavior when meeting a null value in a java map, controls whether the parser should
132+
* write a JSON entry with a null value (<code>isUseNativeNulls() == true</code>)
133+
* or ignore that map entry (<code>isUseNativeNulls() == false</code>).
133134
*
134-
* @return The <code>javaNullAsJsonNull</code> configuration value.
135+
* @return The <code>useNativeNulls</code> configuration value.
135136
*/
136-
public boolean isJavaNullAsJsonNull() {
137-
return this.javaNullAsJsonNull;
137+
public boolean isUseNativeNulls() {
138+
return this.useNativeNulls;
138139
}
139140

140141

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public void jsonArrayByListWithNestedNullValue() {
235235
Map<String, Object> sub = new HashMap<String, Object>();
236236
sub.put("nullKey", null);
237237
list.add(sub);
238-
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withJavaNullAsJsonNull(true);
238+
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
239239
JSONArray jsonArray = new JSONArray(list, parserConfiguration);
240240
JSONObject subObject = jsonArray.getJSONObject(0);
241241
assertTrue(subObject.has("nullKey"));

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ public void jsonObjectByMapWithNullValueAndParserConfiguration() {
627627
assertTrue("expected null value to be ignored by default", obj1.isEmpty());
628628

629629
// if configured, null values are written as such into the JSONObject.
630-
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withJavaNullAsJsonNull(true);
630+
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
631631
JSONObject obj2 = new JSONObject(map, parserConfiguration);
632632
assertFalse("expected null value to accepted when configured", obj2.isEmpty());
633633
assertTrue(obj2.has("nullKey"));
@@ -644,7 +644,7 @@ public void jsonObjectByMapWithNestedNullValueAndParserConfiguration() {
644644
nestedList.add(nestedMap);
645645
map.put("nestedList", nestedList);
646646

647-
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withJavaNullAsJsonNull(true);
647+
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
648648
JSONObject jsonObject = new JSONObject(map, parserConfiguration);
649649

650650
JSONObject nestedObject = jsonObject.getJSONObject("nestedMap");

0 commit comments

Comments
 (0)