0
public class ParentData :ObservableRangeCollection<ChildData>
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }

}

public class ChildData
{
    public string ChildName { get; set; }
    public int ChildAge { get; set; }
}


List<ParentData> lstParent = new List<ParentData>();

ParentData pData = new ParentData();
pData.Name = "John";
pData.Id = 111;
pData.Age = 45;

ChildData childData = new ChildData();
childData.ChildAge = 12;
childData.ChildName = "tommy";

                    
pData.Add(childData);

lstParent.Add(pData);

string Json = JsonConvert.SerializeObject(lstParent, Formatting.None);

Upon serializing, the ParentData does not get serialized

[[{"ChildName":"tommy","ChildAge":12,"ErrorMessage":null}]]

How do I serialize the Parent Node?

Matthew
  • 24,703
  • 9
  • 76
  • 110
MainakChoudhury
  • 502
  • 1
  • 7
  • 23
  • Try not extending the ObservableRangeCollection, and instead have a property of type ObservableRangeCollection called Children and add to that. Most likely this is due to ObservableRangeCollection being a collection, so the serializer had to pick whether i should serialize as an object or an array. – Matthew Feb 02 '21 at 21:43
  • @Matthew can't quite change that... its an existing implementation .. how do i serialize with this extension ? – MainakChoudhury Feb 02 '21 at 21:47
  • Can you provide how you expect the json to look? – Matthew Feb 02 '21 at 21:48
  • @Matthew [{"Id":111,"Name":"John","Age":45,"childDatas":[{"ChildName":"tommy","ChildAge":12,"ErrorMessage":null}]}] – MainakChoudhury Feb 02 '21 at 22:03

0 Answers0