mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Use better name for parser configuration option, fix API comment.
This commit is contained in:
parent
5d1c789490
commit
1afd7cd6bc
@ -332,7 +332,7 @@ public class JSONObject {
|
|||||||
throw new NullPointerException("Null key.");
|
throw new NullPointerException("Null key.");
|
||||||
}
|
}
|
||||||
final Object value = e.getValue();
|
final Object value = e.getValue();
|
||||||
if (value != null || jsonParserConfiguration.isJavaNullAsJsonNull()) {
|
if (value != null || jsonParserConfiguration.isUseNativeNulls()) {
|
||||||
testValidity(value);
|
testValidity(value);
|
||||||
this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration));
|
this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration));
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,9 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
private boolean overwriteDuplicateKey;
|
private boolean overwriteDuplicateKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to indicate whether ignore null values when converting java maps to JSONObject or not.
|
* Used to indicate whether to convert java null values to JSONObject.NULL or ignoring the entry when converting java maps.
|
||||||
*/
|
*/
|
||||||
private boolean javaNullAsJsonNull;
|
private boolean useNativeNulls;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration with the default values.
|
* Configuration with the default values.
|
||||||
@ -74,16 +74,16 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controls the parser's behavior when meeting duplicate keys.
|
* Controls the parser's behavior when meeting Java null values while converting maps.
|
||||||
* If set to false, the parser will throw a JSONException when meeting a duplicate key.
|
* If set to true, the parser will put a JSONObject.NULL into the resulting JSONObject.
|
||||||
* Or the duplicate key's value will be overwritten.
|
* Or the map entry will be ignored.
|
||||||
*
|
*
|
||||||
* @param javaNullAsJsonNull define, if the parser should ignore null values in Java maps
|
* @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.
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
*/
|
*/
|
||||||
public JSONParserConfiguration withJavaNullAsJsonNull(final boolean javaNullAsJsonNull) {
|
public JSONParserConfiguration withUseNativeNulls(final boolean useNativeNulls) {
|
||||||
JSONParserConfiguration clone = this.clone();
|
JSONParserConfiguration clone = this.clone();
|
||||||
clone.javaNullAsJsonNull = javaNullAsJsonNull;
|
clone.useNativeNulls = useNativeNulls;
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
@ -128,13 +128,14 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parser's behavior when meeting a null value in a java map, controls whether the parser should ignore
|
* The parser's behavior when meeting a null value in a java map, controls whether the parser should
|
||||||
* that map entry or write a JSON entry with a null value.
|
* write a JSON entry with a null value (<code>isUseNativeNulls() == true</code>)
|
||||||
|
* or ignore that map entry (<code>isUseNativeNulls() == false</code>).
|
||||||
*
|
*
|
||||||
* @return The <code>javaNullAsJsonNull</code> configuration value.
|
* @return The <code>useNativeNulls</code> configuration value.
|
||||||
*/
|
*/
|
||||||
public boolean isJavaNullAsJsonNull() {
|
public boolean isUseNativeNulls() {
|
||||||
return this.javaNullAsJsonNull;
|
return this.useNativeNulls;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -235,7 +235,7 @@ public class JSONArrayTest {
|
|||||||
Map<String, Object> sub = new HashMap<String, Object>();
|
Map<String, Object> sub = new HashMap<String, Object>();
|
||||||
sub.put("nullKey", null);
|
sub.put("nullKey", null);
|
||||||
list.add(sub);
|
list.add(sub);
|
||||||
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withJavaNullAsJsonNull(true);
|
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
|
||||||
JSONArray jsonArray = new JSONArray(list, parserConfiguration);
|
JSONArray jsonArray = new JSONArray(list, parserConfiguration);
|
||||||
JSONObject subObject = jsonArray.getJSONObject(0);
|
JSONObject subObject = jsonArray.getJSONObject(0);
|
||||||
assertTrue(subObject.has("nullKey"));
|
assertTrue(subObject.has("nullKey"));
|
||||||
|
@ -627,7 +627,7 @@ public class JSONObjectTest {
|
|||||||
assertTrue("expected null value to be ignored by default", obj1.isEmpty());
|
assertTrue("expected null value to be ignored by default", obj1.isEmpty());
|
||||||
|
|
||||||
// if configured, null values are written as such into the JSONObject.
|
// if configured, null values are written as such into the JSONObject.
|
||||||
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withJavaNullAsJsonNull(true);
|
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
|
||||||
JSONObject obj2 = new JSONObject(map, parserConfiguration);
|
JSONObject obj2 = new JSONObject(map, parserConfiguration);
|
||||||
assertFalse("expected null value to accepted when configured", obj2.isEmpty());
|
assertFalse("expected null value to accepted when configured", obj2.isEmpty());
|
||||||
assertTrue(obj2.has("nullKey"));
|
assertTrue(obj2.has("nullKey"));
|
||||||
@ -644,7 +644,7 @@ public class JSONObjectTest {
|
|||||||
nestedList.add(nestedMap);
|
nestedList.add(nestedMap);
|
||||||
map.put("nestedList", nestedList);
|
map.put("nestedList", nestedList);
|
||||||
|
|
||||||
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withJavaNullAsJsonNull(true);
|
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
|
||||||
JSONObject jsonObject = new JSONObject(map, parserConfiguration);
|
JSONObject jsonObject = new JSONObject(map, parserConfiguration);
|
||||||
|
|
||||||
JSONObject nestedObject = jsonObject.getJSONObject("nestedMap");
|
JSONObject nestedObject = jsonObject.getJSONObject("nestedMap");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user