Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonConvert.SerializeObject: Unexpected result when Serializing null value

In the line of code below, my string x ends up being an actual string "null" when clInitializer.AVOptions = null value:

string x = JsonConvert.SerializeObject(clInitializer.AVOptions, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

As in actual word "null" and not a null value or perhaps {}. I am not sure this is expected behavior. Does anyone know how to make it not return the word "null", or do I have some fundamental misunderstanding on how JsonConvert works. Thank you in advance.

like image 928
concentriq Avatar asked Oct 31 '17 03:10

concentriq


People also ask

How to ignore null values when serializing JSON data?

Alternatively you can ignore null values when you serialize. string json = JsonConvert.SerializeObject (employee, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

How do I serialize an object in JSON?

SerializeObject(Object, Formatting, JsonSerializerSettings) Serializes the specified object to a JSON string using formatting and JsonSerializerSettings. Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings.

How to deserialize object without [jsonproperty] attribute in JSON?

JsonConvert.DeserializeObject can leave reference type member properties null during deserialization without [JsonProperty] attribute on the property. 1. Synchronize the property name in the class and in the JSON file.

What is jsonserializersettings in Java?

Serializes the specified object to a JSON string using JsonSerializerSettings . Serializes the specified object to a JSON string using formatting and a collection of JsonConverter .


2 Answers

The NullValueHandling has to do with null values for a property and not the object it self.

For example, if you have the below example:

public class ExampleClass
{
    public string NullProperty { get; set; }
}

And then you serialize it:

var obj = new ExampleClass();
var jsons = JsonConvert.SerializeObject(obj, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });

Then the NullProperty is ignored and you get {}.

Edited

The reason why "null" is returned is because the RFC for JSON (https://www.rfc-editor.org/rfc/rfc7159) explicitly states the following:

A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:

false null true

The literal names MUST be lowercase. No other literal names are
allowed.

value = false / null / true / object / array / number / string

false = %x66.61.6c.73.65 ; false

null = %x6e.75.6c.6c ; null

true = %x74.72.75.65 ; true

Edited:

I originally had a work around, but I removed it because I really think you should follow the RFC. The RFC clearly stated that a NULL object MUST be represented by a "null" so any work around is not a good idea.

To stay with the RFC, I would store "null" or return "null" and not NULL. When you deserialize "null" it will return a NULL value.

like image 160
Rogala Avatar answered Sep 18 '22 11:09

Rogala


A value of null will always return the null string.

JsonConvert.SerializeObject(null, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

Will return the string null.

If you were to pass in the object clInitializer that has a null AVOptions then with your JsonSerializerSettings it would Ignore the property AVOptions and not have that in the string. If your clInitializer object only had the property AVOptions (or only null properties) then you would get {} returned when passing in clInitializer.

like image 33
Cedar Avatar answered Sep 16 '22 11:09

Cedar