Assuming I have an array of the following JSON data:
[
{
"foo": "somestring",
"foo1": 2,
"foo2": [
"somestring1",
"somestring2"
],
"foo3": [
{
"bar1": 122312,
"bar2": 3214324
},
{
"bar3": 432432432,
"bar4": 4324231
}
]
},
//more root array members...
]
I have created the following classes for the JSON to be deserialized into:
internal class StoredFoo
{
[JsonPropertyName("foo")]
public string Foo
{
get; set;
}
[JsonPropertyName("foo1")]
public double Foo1
{
get; set;
}
[JsonPropertyName("foo2")]
public List<string> Foo2 { get; } = new List<string>();
[JsonPropertyName("foo3")]
public List<StoredBar> Foo3 { get; } = new List<StoredBar>();
}
internal class StoredBar
{
[JsonPropertyName("bar1")]
public long Bar1
{
get; set;
}
[JsonPropertyName("bar2")]
public long Bar2
{
get; set;
}
}
(I apologize for being unable to use real names. part of the limitations of the project)
When I load up the JSON file and deserialize it:
string jsonFooData = File.ReadAllText(filePath);
IEnumerable<Foo> storedFoos = JsonSerializer.Deserialize<IEnumerable<Foo>>(jsonFooData);
Then storedFoos
contains the objects, but Foo2
and Foo3
are empty, even when they shouldn't.
At first, I thought that this is because somehow the = new List<string>()
occurs after the JsonSerializer
had initialized the collection, effectively overriding it. But even removing the = new List<string>()
statement, the collections are just set to null
(without exception).
Why is that that JsonSerializer
succeeds in deserializing into foo
and foo1
but not foo2
and foo3
?