Hopefully a simple test case comparing System.Text.Json with Newtonsoft.Json:
```c#
using System.Collections.Generic;
namespace Test
{
public class Foo
{
public string Name;
public List
}
public class Bar
{
public string Location;
}
class Program
{
static void Main(string[] args)
{
var foos = new List<Foo>();
var oneBar = new Bar {
Location = "Somewhere"
};
var oneFoo = new Foo();
oneFoo.Name = "Test";
oneFoo.Bars.Add(oneBar);
foos.Add(oneFoo);
var test1 = System.Text.Json.JsonSerializer.Serialize(foos);
var test2 = Newtonsoft.Json.JsonConvert.SerializeObject(foos);
}
}
}
```
Why does test1 yield [{}] whilst test2 yields (the expected) [{"Name":"Test","Bars":[{"Location":"Somewhere"}]}] ? Am I missing something crucial here?
I'm having this same issue where any nested object in a list yields empty while deserializing as well. So a call like System.Text.Json.JsonSerializer.Parse
System.Text.Json, for its first release doesn't support serializing/deserializing fields.
If you change your Foo object to have public properties, it should work as expected.
```C#
public class Foo
{
public string Name { get; set; }
public List
}
public class Bar
{
public string Location { get; set; }
}
[Fact]
public static void BlahBlah ()
{
var foos = new List
var oneBar = new Bar
{
Location = "Somewhere"
};
var oneFoo = new Foo();
oneFoo.Name = "Test";
oneFoo.Bars = new List<Bar>(); // note the diff here
oneFoo.Bars.Add(oneBar);
foos.Add(oneFoo);
var test1 = System.Text.Json.JsonSerializer.Serialize(foos);
var test2 = Newtonsoft.Json.JsonConvert.SerializeObject(foos);
Assert.Equal(test2, test1);
}
```
See https://github.com/dotnet/corefx/issues/36505 / https://github.com/dotnet/corefx/pull/39254 where we plan to add support for fields (for vNext).
We have a doc you can refer to (currently in PR) for high-level list of things that are supported/not supported:
https://github.com/dotnet/corefx/pull/38413
I'm having this same issue where any nested object in a list yields empty while deserializing as well.
@LoganGirard, does your issue fall in the same category? If not, can you provide a simplified repro of the issue?
My issue was having a class like:
```c#
public class Foo
{
public string Name { get; set; }
public List
}
public class Bar
{
public string Location { get; set; }
}
```c#
var json = @"{
"Bars": [{
"Location": "loc"
}]
}"
var foo = System.Text.Json.JsonSerializer.Deserialize<Foo>(json);
// foo.Bars undefined despite having data
This seems to have resolved since upgrading from preview 6 to 7.
Sounds good. Hence, closing.