diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java
index d50fff7..a1664f7 100644
--- a/src/main/java/org/json/JSONObject.java
+++ b/src/main/java/org/json/JSONObject.java
@@ -332,7 +332,7 @@ public class JSONObject {
throw new NullPointerException("Null key.");
}
final Object value = e.getValue();
- if (value != null) {
+ if (value != null || jsonParserConfiguration.isUseNativeNulls()) {
testValidity(value);
this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration));
}
diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java
index 3fbfb8a..0cfa2ea 100644
--- a/src/main/java/org/json/JSONParserConfiguration.java
+++ b/src/main/java/org/json/JSONParserConfiguration.java
@@ -8,6 +8,11 @@ public class JSONParserConfiguration extends ParserConfiguration {
* Used to indicate whether to overwrite duplicate key or not.
*/
private boolean overwriteDuplicateKey;
+
+ /**
+ * Used to indicate whether to convert java null values to JSONObject.NULL or ignoring the entry when converting java maps.
+ */
+ private boolean useNativeNulls;
/**
* Configuration with the default values.
@@ -32,6 +37,7 @@ public class JSONParserConfiguration extends ParserConfiguration {
clone.strictMode = strictMode;
clone.maxNestingDepth = maxNestingDepth;
clone.keepStrings = keepStrings;
+ clone.useNativeNulls = useNativeNulls;
return clone;
}
@@ -67,6 +73,21 @@ public class JSONParserConfiguration extends ParserConfiguration {
return clone;
}
+
+ /**
+ * Controls the parser's behavior when meeting Java null values while converting maps.
+ * If set to true, the parser will put a JSONObject.NULL into the resulting JSONObject.
+ * Or the map entry will be ignored.
+ *
+ * @param useNativeNulls defines if the parser should convert null values in Java maps
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public JSONParserConfiguration withUseNativeNulls(final boolean useNativeNulls) {
+ JSONParserConfiguration clone = this.clone();
+ clone.useNativeNulls = useNativeNulls;
+
+ return clone;
+ }
/**
* Sets the strict mode configuration for the JSON parser with default true value
@@ -106,6 +127,18 @@ public class JSONParserConfiguration extends ParserConfiguration {
public boolean isOverwriteDuplicateKey() {
return this.overwriteDuplicateKey;
}
+
+ /**
+ * The parser's behavior when meeting a null value in a java map, controls whether the parser should
+ * write a JSON entry with a null value (isUseNativeNulls() == true
)
+ * or ignore that map entry (isUseNativeNulls() == false
).
+ *
+ * @return The useNativeNulls
configuration value.
+ */
+ public boolean isUseNativeNulls() {
+ return this.useNativeNulls;
+ }
+
/**
* The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters.
diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java
index 580fe82..4296203 100644
--- a/src/test/java/org/json/junit/JSONArrayTest.java
+++ b/src/test/java/org/json/junit/JSONArrayTest.java
@@ -228,6 +228,19 @@ public class JSONArrayTest {
Util.checkJSONArrayMaps(jaRaw);
Util.checkJSONArrayMaps(jaInt);
}
+
+ @Test
+ public void jsonArrayByListWithNestedNullValue() {
+ List