首页 新闻 搜索 专区 学院

用System.Text.Json序列化有子项和接口的实体类中子项目却缺属性是什么情况?

0
悬赏园豆:5 [待解决问题]

我用的是.Net Core 3.1 C#
实体类的代码是这样的

public class NewsColumnTreeListViewModels : IToTreeModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int NewsCount { get; set; }
        public int ParentId { get; set; }
        public List<IToTreeModel> children { get; set; }
        public void AddChilrden(IToTreeModel node)
        {
            if (children == null)
                children = new List<IToTreeModel>();
            children.Add(node);
        }
    }

接口:

    public interface IToTreeModel
    {
        int Id { get; set; }
        int ParentId { get; set; }
        List<IToTreeModel> children { get; set; }
        void AddChilrden(IToTreeModel node);
    }

JsonSerializer.Serialize(list);序列化后第一层数据是完整的,第二次之后之序列化了接口中的属性,接口没有的属性不会进行序列化。
变成了这样:

[{
    "id": 1,
    "name": "\u6D4B\u8BD51",
    "newsCount": 0,
    "parentId": 0,
    "children": [{
        "id": 5,
        "parentId": 1,
        "children": [{
            "id": 7,
            "parentId": 5,
            "children": null
        }, {
            "id": 9,
            "parentId": 5,
            "children": [{
                "id": 10,
                "parentId": 9,
                "children": null
            }, {
                "id": 11,
                "parentId": 9,
                "children": null
            }]
        }]
    }]
}, {
    "id": 2,
    "name": "\u6D4B\u8BD52",
    "newsCount": 0,
    "parentId": 0,
    "children": [{
        "id": 6,
        "parentId": 2,
        "children": null
    }]
}]

吧实体类改成这样就没问题了

    public class NewsColumnTreeListViewModels
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int NewsCount { get; set; }
        public int ParentId { get; set; }
        public List<NewsColumnTreeListViewModels> children { get; set; }
        public void AddChilrden(NewsColumnTreeListViewModels node)
        {
            if (children == null)
                children = new List<NewsColumnTreeListViewModels>();
            children.Add(node);
        }
    }

用的是同一个方法载入的数据,这个方法一开始是泛型的 我只是把泛型去掉了,带接口和不带接口的实体用的都是同一个去掉泛型之后的方法
调试模式确认了,序列化前的数据是完整的(就算不完整也应该是null)

是我写的有问题还是发现了Bug????

问题补充:

复现问题的代码:

        public IActionResult Privacy()
        {
            var data1 = new List<Data1>();
            //Data1
            var d15 = new Data1() { Id = 5, Name = "NameData1-5", Text = "TextData1-5" };
            var d14 = new Data1() { Id = 4, Name = "NameData1-4", Text = "TextData1-4" };
            var list11 = new List<IDate>();
            list11.Add(d15);
            list11.Add(d14);
            var d13 = new Data1() { Id = 3, Name = "NameData1-3", Text = "TextData1-3", children = list11 };
            var list12 = new List<IDate>();
            list12.Add(d13);
            var d12 = new Data1() { Id = 2, Name = "NameData1-2", Text = "TextData1-2", children = list12 };
            var d11 = new Data1() { Id = 1, Name = "NameData1-1", Text = "TextData1-1" };
            data1.Add(d11);
            data1.Add(d12);

            //Data2
            var data2 = new List<Data2>();
            var d25 = new Data2() { Id = 5, Name = "NameData2-5", Text = "TextData2-5" };
            var d24 = new Data2() { Id = 4, Name = "NameData2-4", Text = "TextData2-4" };
            var list21 = new List<Data2>();
            list21.Add(d25);
            list21.Add(d24);
            var d23 = new Data2() { Id = 3, Name = "NameData2-3", Text = "TextData2-3", children = list21 };
            var list22 = new List<Data2>();
            list22.Add(d23);
            var d22 = new Data2() { Id = 2, Name = "NameData2-2", Text = "TextData2-2", children = list22 };
            var d21 = new Data2() { Id = 1, Name = "NameData2-1", Text = "TextData2-1" };
            data2.Add(d21);
            data2.Add(d22);

            string s1 = System.Text.Json.JsonSerializer.Serialize(data1);
            string s2 = System.Text.Json.JsonSerializer.Serialize(data2);

            ViewBag.s1 = s1;
            ViewBag.s2 = s2;

            return View();
        }

实体类:
····
public class Data1 : IDate
{
public int Id { get; set; }
public string Name { get; set; }
public List<IDate> children { get; set; }

    public string Text { get; set; }
}

public interface IDate
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<IDate> children { get; set; }
}

public class Data2
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Data2> children { get; set; }

    public string Text { get; set; }
}

····
最后结果:
S1=
····
[{"Id":1,"Name":"NameData1-1","children":null,"Text":"TextData1-1"},{"Id":2,"Name":"NameData1-2","children":[{"Id":3,"Name":"NameData1-3","children":[{"Id":5,"Name":"NameData1-5","children":null},{"Id":4,"Name":"NameData1-4","children":null}]}],"Text":"TextData1-2"}]
····
S2=
····
[{"Id":1,"Name":"NameData2-1","children":null,"Text":"TextData2-1"},{"Id":2,"Name":"NameData2-2","children":[{"Id":3,"Name":"NameData2-3","children":[{"Id":5,"Name":"NameData2-5","children":null,"Text":"TextData2-5"},{"Id":4,"Name":"NameData2-4","children":null,"Text":"TextData2-4"}],"Text":"TextData2-3"}],"Text":"TextData2-2"}]

····

随缘py的主页 随缘py | 初学一级 | 园豆:146
提问于:2020-03-19 01:01

为了便于回答者调试,建议提供重现问题的示例代码

dudu 1年前

@dudu: 补上了~

随缘py 1年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册