Yes, parsing valid JSON "null" with a JSON serializer have to return null.
WeatherForecast? weatherForecast =
JsonSerializer.Deserialize<WeatherForecast>("null");
Note that other valid JSON strings like "123", "\"bob\"", "[]" should cause an exception because none of them represent a valid object.
i have a list of records with the following definition. basically a IEnumerable<MyComponent>. The MyComponent record has two properties with the type JsonElement. They usually are a Json object with multiple properties of various data types.
Now i want to serialize the IEnumerable<MyComponent> so that all null values in the nested objects of Attributes and Internals are removed. basically the serializer should only return the Json with the attributes which have an explicit value. Unfortunately this seems to be a harder problem than expected.
I tried to use JsonIgnoreCondition.WhenWritingNull but this setting doesn't care about the nested properties inside of Attributes and Internals. Does anyone have good solution for this?
public record MyComponent
{
public required string Uid { get; init; }
public JsonElement? Attributes { get; init; }
public JsonElement? Internals { get; init; }
}example:
basically this:
[{
"Uid": 10,
"Attributes": {
"a": 10,
"b": null,
"c": "test"
},
"Internals": {
"x": "int1",
"y": ["test", "blub"],
"z": null
}
}]
should result in:
[{
"Uid": 10,
"Attributes": {
"a": 10,
"c": "test"
},
"Internals": {
"x": "int1",
"y": ["test", "blub"],
}
}]Using:
-
.net 8
-
System.Text.Json serializer.
Let's say I have a two classes defined like this:
public class SampleClass
{
public SampleClassDetail? FirstProperty { get; set; }
public SampleClassDetail? SecondProperty { get; set; }
}
public class SampleClassDetail
{
public int MyProperty { get; set; }
}And an instance of `SampleClass` :
SampleClass sample = new()
{
FirstProperty = new SampleClassDetail { MyProperty = 1 }
};Then I want to serialize it with `System.Text.Json`:
string serialized = JsonSerializer.Serialize(sample);
This produces:
{
"FirstProperty": {
"MyProperty": 1
},
"SecondProperty": null
}But I would the null to be treated like: `"SecondProperty": {}`
I've tried creating a CustomJson Converter and adding it to the options:
public class SampleClassDetailConverter : JsonConverter<SampleClassDetail?>
{
public override SampleClassDetail? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, SampleClassDetail? value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteStartObject();
writer.WriteEndObject();
}
else
{
var newOptions = new JsonSerializerOptions(options);
newOptions.Converters.Remove(this);
JsonSerializer.Serialize(writer, value, newOptions);
}
}
}
var options = new JsonSerializerOptions();
options.Converters.Add(new SampleClassDetailConverter());
string serialized = JsonSerializer.Serialize(sample, options);But still getting the same.
I've created a dotnetfiddle:
https://dotnetfiddle.net/PsXrxt
Any idea how to achieve this?