Runtime: Serializing List<object>'s yields empty output

Created on 24 Jul 2019  路  5Comments  路  Source: dotnet/runtime

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 Bars = new 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?

area-System.Text.Json json-functionality-doc

All 5 comments

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(fooObj) doesn't deserialize my list of Bar. I'm on preview 6 on .net core 3

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 Bars { get; set; }
}

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 Bars { get; set; }
}

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.

Was this page helpful?
0 / 5 - 0 ratings