-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Text.Json: don't emit null values when serializing collections #682
Comments
Yep. When using From @steveharter
So, this begs the questions, what is your scenario in which you need to ignore null values in collections and do you need per-property/collection granularity to turn this feature on, or is a global switch what you need? Also, why is ignoring nulls in collections, and hence changing length/count as a side effect, not a concern for your use case? |
There are several options. Don't add null-valued entries to a dictionary, or filter them out after adding, as well as using custom converters or even ShouldSerialize... methods. A cursory search in Stack Overflow finds many posts - so if we have an option to improve what seems to be a common scenario/question, we could take it.
We have at the moment multiple scenarios where On granularity, a JsonSerializerOptions property would probably be preferable, allowing the developer to decide if a global options are followed or apply the behavior just to specific cases. |
From @JamesNK
Even given all the correctness and round-tripping considerations, one may desire to exclude null colleciton elements when serializing to reduce JSON size or for other reasons, and not pay the cost of pre-filtering. If a global and/or per-property option is added, should it apply to deserialization as well, i.e. don't add null elements to my collections? |
From @CodeBlanch in #31786
public class TestClass
{
public string Prop1 { get; set; } = "value";
public string Prop2 { get; set; }
}
{"Prop1":"value"}
public class TestClass
{
[JsonExtensionData]
public IDictionary<string, object> Fields { get; set; } = new Dictionary<string, object>
{
["Prop1"] = "value",
["Prop2"] = null
}
}
{"Prop1":"value","Prop2":null} |
The Changing this would be a breaking change. For example: public static void Main()
{
var options = new JsonSerializerOptions { IgnoreNullValues = true };
var obj = new TestClass();
// {"List":["a",null,"b"]}
Console.WriteLine(JsonSerializer.Serialize(obj, options));
// {"List":["a",null,"b"]}
Console.WriteLine(JsonConvert.SerializeObject(obj, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
}
public class TestClass
{
public List<string> List { get; set; } = new List<string> { "a", null, "b" };
} See: #29495 (comment) |
Closing per feedback. |
System.Text.Json has JsonSerializerOptions.IgnoreNullValues, but this only applies to properties, and will not ignore values in collections.
For example:
Output is:
(DbNull.Value has #418 open already).
Expectation for
IgnoreNullValues = true
is that all null values would be ignored and not serialized, not just properties. However, this would be a breaking change from JSON.NET, which has the same behavior withNullValueHandling.Ignore
, so probably a new JsonSerializerOptions property is needed (no idea what it could be named).This is especially useful in a very common scenario (1, 2, 3 etc) when data is fetched from database with SqlDataReader as
List<Dictionary<string, object>>
.The text was updated successfully, but these errors were encountered: