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 | Show 1 more comment1 Answer
Reset to default 0In 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.
Name
andSize
to be repeated for every element? (That sounds very misleading to me...) – Jon Skeet Commented Feb 15 at 10:22TestClassCollection
type... – Jon Skeet Commented Feb 15 at 12:18