Reputation: 2203
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?
Upvotes: 2
Views: 6166
Reputation:
You can use the JsonExtensionDataAttribute that JSON.Net provides to put any properties that don't exist on the parent object in to the collection property.
Here's an example taken from another SO post.
JSON:
{
"X" : "value",
"key1": "value1",
"key2": "value2"
}
C# Data Object:
public class Test
{
public string X { get; set; }
[JsonExtensionData]
public Dictionary<string, object> Y { get; set; }
}
Key1 and Key2 will end up in the Dictionary because they don't exist in the class "Test, where as X will end up in the X property.
It's not perfect, it seems you have to have a Dictionary of type Dictionary, but it still might be a better option that using a custom serializer?
Here's an example on DotNetFiddle using one level of your data classes.
For the data:
public class UserData
{
public string UserName;
public string UserPhone;
}
public class CustomerData
{
public string CustomerName;
[JsonExtensionData]
public Dictionary<string, object> Users;
}
var data = new CustomerData()
{
CustomerName = "Foo",
Users = new Dictionary<string, object>()
{
{ "1", new UserData() { UserName = "Fireman", UserPhone = "0118 999 881 999 119 725 ... 3" } },
{ "2", new UserData() { UserName = "Jenny", UserPhone = "867-5309" } }
}
};
It produces the JSON:
{
"customerName": "Foo",
"1": {
"userName": "Fireman",
"userPhone": "0118 999 881 999 119 725 ... 3"
},
"2": {
"userName": "Jenny",
"userPhone": "867-5309"
}
}
Upvotes: 5