最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - JSON serializing object which inherits from ObservableCollection - How to include properties of child class? - Stack Overfl

programmeradmin4浏览0评论

I am using Json to save some data:

public ViewModel
{
    public TestClass testClass { get; set; }

    public ViewModel()
    {
        testClass = new TestClass();
    }

    public void SaveFile()
    {
        SaveAs(testClass);
    }

    private void SaveAs(object data)
    {
        string jsonString = JsonSerializer.Serialize(data);

        SaveFileDialog dialog = new SaveFileDialog();
        if (dialog.ShowDialog() == true)
        {
            File.WriteAllText(dialog.FileName,jsonString);
        }
    }
}

When I make a simple class like this:

public class TestClass
{
    public string Name { get; set; }
    public double Size { get; set; }
}

The serialization works as intended, and gives me this file:

{
  "Name": null,
  "Size": 0
}

When I want my class to inherit from ObservableCollection like this:

public class TestClass : ObservableCollection<object>
{
    public TestClass()
    {
        Items.Add(new TestObject());
    }

    public string Name { get; set; }
    public double Size { get; set; }
}

public class TestObject
{
    public string ObjectName { get; set; }
    public double ObjectSize { get; set; }
}

The serialization only gives me the objects of the ObservableCollection:

[
  {
    "ObjectName": null,
    "ObjectSize": 0
  }
]

I want the Name and Size properties of the TestClass class to be serialized as well. How can I make this happen?

I am using Json to save some data:

public ViewModel
{
    public TestClass testClass { get; set; }

    public ViewModel()
    {
        testClass = new TestClass();
    }

    public void SaveFile()
    {
        SaveAs(testClass);
    }

    private void SaveAs(object data)
    {
        string jsonString = JsonSerializer.Serialize(data);

        SaveFileDialog dialog = new SaveFileDialog();
        if (dialog.ShowDialog() == true)
        {
            File.WriteAllText(dialog.FileName,jsonString);
        }
    }
}

When I make a simple class like this:

public class TestClass
{
    public string Name { get; set; }
    public double Size { get; set; }
}

The serialization works as intended, and gives me this file:

{
  "Name": null,
  "Size": 0
}

When I want my class to inherit from ObservableCollection like this:

public class TestClass : ObservableCollection<object>
{
    public TestClass()
    {
        Items.Add(new TestObject());
    }

    public string Name { get; set; }
    public double Size { get; set; }
}

public class TestObject
{
    public string ObjectName { get; set; }
    public double ObjectSize { get; set; }
}

The serialization only gives me the objects of the ObservableCollection:

[
  {
    "ObjectName": null,
    "ObjectSize": 0
  }
]

I want the Name and Size properties of the TestClass class to be serialized as well. How can I make this happen?

Share Improve this question edited Feb 15 at 15:45 mmdn asked Feb 15 at 9:49 mmdnmmdn 556 bronze badges 6
  • What would you even expect the JSON to look like? A JSON value can't be both a collection and an object with properties. You should probably either use composition instead of inheritance, or write a custom JSON converter. – Jon Skeet Commented Feb 15 at 9:55
  • @JonSkeet see my edit – mmdn Commented Feb 15 at 10:08
  • That's not valid JSON - and what would you expect if there were multiple elements in the collection? Would you expect Name and Size to be repeated for every element? (That sounds very misleading to me...) – Jon Skeet Commented Feb 15 at 10:22
  • I see what you mean now. I revised my Edit to show how I could set it up instead, to achieve the json-serialization I am looking for. Thanks for replying. Should I post my "edit" as an answer instead? – mmdn Commented Feb 15 at 12:03
  • I'd note that at this point there doesn't really seem to be much point in having the TestClassCollection type... – Jon Skeet Commented Feb 15 at 12:18
 |  Show 1 more comment

1 Answer 1

Reset to default 0

In order to achieve what is being asked, ObservableCollection<> should be a property in the TestClass, rather than a parent.

Like this:

public class TestClass
{
    public TestClass()
    {
        testObjects = new TestClassCollection();
    }

    public TestClassCollection testObjects { get; set; }

    public string Name { get; set; }
    public double Size { get; set; }
}

public class TestClassCollection : ObservableCollection<TestObject>
{
    public TestClassCollection()
    {
        Items.Add(new TestObject());
    }
}

public class TestObject
{
    public string ObjectName { get; set; }
    public double ObjectSize { get; set; }
}

Which gives following upon serialization:

{
  "testObjects": [
    {
      "ObjectName": null,
      "ObjectSize": 0
    }
  ],
  "Name": null,
  "Size": 0
}

The point of having the TestClassCollection in the first place, would be to override methods in ObservableCollection<>, which is not shown in this example.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论