Closed haroon-sheikh closed 4 years ago
Hi @haroon-sheikh
I have some questions. You are setting serializeNulls on the Gson Object Mapper. That setting will make sure that when you have a Java Object and it has a null value, it will serialize it as null.
But the object mapper is not used by the Unirest Code you are showing. You aren't posting an object to the user, and you are not mapping the response to an object. asJson
does not use the ObjectMapper because it's not mapping the response to an Object, only to a generic JSON Tree
note that serializeNulls doesn't really have anything to mapping JSON to an object, it is used when serializing an object to a JSON string. Again, you are not posting an object so this would never happen. Per the Google documentation:
/**
* Configure Gson to serialize null fields. By default, Gson omits all fields that are null
* during serialization.
*/
Thanks for looking into it @ryber.
I would like to have the null values in the Java object. Any ideas what would be the easiest/best approach for achieving this?
I thought just creating Gson gson = new GsonBuilder().serializeNulls().create();
and passing to the custom mapper would work. But obviously i'm missing something.
If I understand it correctly, i would need to map it to object asObject
along with withObjectMapper
?
In what Java Object? the JsonNode? It does have the key, it's just null and if you toString it it will omit the key, but it does exist in there:
@Test
public void nullObjectsOnParse() {
JSONObject obj = new JSONObject("{\"key1\": \"value\", \"key2\": null}");
assertTrue(obj.has("key2"));
assertEquals(null, obj.get("key2"));
}
are you asking for the JsonNode's toString operation to include nulls?
Yep, jsonnode.getBody()
to include nulls
in the String.
If all you are interested in is the string you could just call .asString().getBody()
and get the raw JSON body. There is no need to go through some library if all you want it raw JSON.
If all you are interested in is the string you could just call
.asString().getBody()
and get the raw JSON body. There is no need to go through some library if all you want it raw JSON.
That makes sense. We're in middle of applying an update to Unirest version from 1 to 3. The problem we have is that the same method is expecting a json object as well as raw string. And the removal of getRawBody in the latest versions isn't helping. We'll put together a workaround until, the changes can be refactored to work separately with both.
Thank you for your support. :)
The JSON implementation implementation in unirest is a cleanroom implementation of org.json and should have all the same behavior. I checked with org.json and found that it does serialize nulls, so I'm reopening this issue to fix that defect
The JSON implementation implementation in unirest is a cleanroom implementation of org.json and should have all the same behavior. I checked with org.json and found that it does serialize nulls, so I'm reopening this issue to fix that defect
3.7.04 is released with the change @haroon-sheikh
Thank you @ryber. It's working as expected.
Describe the bug A clear and concise description of what the bug is.
To Reproduce Steps to reproduce the behavior:
Mapper
Option 1:
Expected =
{"key1": "value", "key2": null}
Actual ={"key1": "value"}
Option 1:
Expected =
{"key1": "value", "key2": null}
Actual ={"key1": "value"}
Screenshots If applicable, add screenshots to help explain your problem.
Environmental Data:
Additional context Add any other context about the problem here.