I use the Newtonsoft JSON serializer, and the serialized string skips properties from the derived class if the class is derived from a list. Here is my sample code.
Classes:
[DataContract] public class TestItem { [DataMember] public int itemInt; [DataMember] public string itemString; public TestItem() {} public TestItem(int _intVal, string _stringVal) { itemInt = _intVal; itemString = _stringVal; } } [DataContract] public class TestMain : List<TestItem> { [DataMember] public int mainInt; [DataMember] public string mainString; }
Serializing code:
string test; // Test classes TestMain main = new TestMain(); main.mainInt = 123; main.mainString = "Hello"; main.Add(new TestItem(1, "First")); test = Newtonsoft.Json.JsonConvert.SerializeObject(main);
After serialization, the test value:
[{\ "itemInt \": 1, \ "itemString \": \ "First \"}]
The values โโof mainInt and mainString are missing at all.
The behavior is not changed by the tags [DataContract] and [DataMember], but I am there to preempt the answer that they lack.
How to get JSON for recognizing and serializing properties of mainInt and mainString of a derived class?
source share