mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Merge pull request #983 from harshith8854/master
Use JSONParserConfiguration to decide on serializing Null fields into JSONObject #982
This commit is contained in:
commit
72a1a48173
@ -401,12 +401,17 @@ public class JSONObject {
|
|||||||
*/
|
*/
|
||||||
public JSONObject(Object bean) {
|
public JSONObject(Object bean) {
|
||||||
this();
|
this();
|
||||||
this.populateMap(bean);
|
this.populateMap(bean, new JSONParserConfiguration());
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject(Object bean, JSONParserConfiguration jsonParserConfiguration) {
|
||||||
|
this();
|
||||||
|
this.populateMap(bean, jsonParserConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject(Object bean, Set<Object> objectsRecord) {
|
private JSONObject(Object bean, Set<Object> objectsRecord) {
|
||||||
this();
|
this();
|
||||||
this.populateMap(bean, objectsRecord);
|
this.populateMap(bean, objectsRecord, new JSONParserConfiguration());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1764,11 +1769,11 @@ public class JSONObject {
|
|||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
* If a getter returned a non-finite number.
|
* If a getter returned a non-finite number.
|
||||||
*/
|
*/
|
||||||
private void populateMap(Object bean) {
|
private void populateMap(Object bean, JSONParserConfiguration jsonParserConfiguration) {
|
||||||
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
|
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()), jsonParserConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void populateMap(Object bean, Set<Object> objectsRecord) {
|
private void populateMap(Object bean, Set<Object> objectsRecord, JSONParserConfiguration jsonParserConfiguration) {
|
||||||
Class<?> klass = bean.getClass();
|
Class<?> klass = bean.getClass();
|
||||||
|
|
||||||
// If klass is a System class then set includeSuperClass to false.
|
// If klass is a System class then set includeSuperClass to false.
|
||||||
@ -1788,7 +1793,7 @@ public class JSONObject {
|
|||||||
if (key != null && !key.isEmpty()) {
|
if (key != null && !key.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
final Object result = method.invoke(bean);
|
final Object result = method.invoke(bean);
|
||||||
if (result != null) {
|
if (result != null || jsonParserConfiguration.isUseNativeNulls()) {
|
||||||
// check cyclic dependency and throw error if needed
|
// check cyclic dependency and throw error if needed
|
||||||
// the wrap and populateMap combination method is
|
// the wrap and populateMap combination method is
|
||||||
// itself DFS recursive
|
// itself DFS recursive
|
||||||
|
@ -4011,5 +4011,37 @@ public class JSONObjectTest {
|
|||||||
nestedMap.put("t", buildNestedMap(maxDepth - 1));
|
nestedMap.put("t", buildNestedMap(maxDepth - 1));
|
||||||
return nestedMap;
|
return nestedMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the behavior of the {@link JSONObject} when parsing a bean with null fields
|
||||||
|
* using a custom {@link JSONParserConfiguration} that enables the use of native nulls.
|
||||||
|
*
|
||||||
|
* <p>This test ensures that uninitialized fields in the bean are serialized correctly
|
||||||
|
* into the resulting JSON object, and their keys are present in the JSON string output.</p>
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void jsonObjectParseNullFieldsWithParserConfiguration() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||||
|
RecursiveBean bean = new RecursiveBean(null);
|
||||||
|
JSONObject jsonObject = new JSONObject(bean, jsonParserConfiguration.withUseNativeNulls(true));
|
||||||
|
assertTrue("name key should be present", jsonObject.has("name"));
|
||||||
|
assertTrue("ref key should be present", jsonObject.has("ref"));
|
||||||
|
assertTrue("ref2 key should be present", jsonObject.has("ref2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the behavior of the {@link JSONObject} when parsing a bean with null fields
|
||||||
|
* without using a custom {@link JSONParserConfiguration}.
|
||||||
|
*
|
||||||
|
* <p>This test ensures that uninitialized fields in the bean are not serialized
|
||||||
|
* into the resulting JSON object, and the object remains empty.</p>
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void jsonObjectParseNullFieldsWithoutParserConfiguration() {
|
||||||
|
RecursiveBean bean = new RecursiveBean(null);
|
||||||
|
JSONObject jsonObject = new JSONObject(bean);
|
||||||
|
assertTrue("JSONObject should be empty", jsonObject.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user