I have the following C# code, using Newtonsoft.Json v6.0.6:
void Main() {
var data=new AllData();
var cst=new CustomerData();
cst.CustomerName="Customer1";
cst.Add(1, new UserData() {UserName="User1", UserPhone="123456789"});
cst.Add(2, new UserData() {UserName="User2", UserPhone="987654321"});
data.Add(1, cst);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
}
public class UserData {
public string UserName;
public string UserPhone;
}
public class CustomerData:Dictionary<int,UserData> {
public string CustomerName;
}
public class AllData:Dictionary<int,CustomerData> {}
So from this code I'm expecting to see this output:
{
"1": {
"CustomerName": "Customer1",
"1": {
"UserName": "User1",
"UserPhone": "123456789"
},
"2": {
"UserName": "User2",
"UserPhone": "987654321"
}
}
}
But instead I'm seeing:
{
"1": {
"1": {
"UserName": "User1",
"UserPhone": "123456789"
},
"2": {
"UserName": "User2",
"UserPhone": "987654321"
}
}
}
i.e. My CustomerName property is ignored.
I've played with the Newtonsoft.Json.MemberSerialization and Newtonsoft.Json.JsonProperty attributes without success and so am wondering where to look now. A full custom serialisation implementation seems like overkill, is there a simple way to solve this?