I have the following C# code:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class Program
{
private static readonly JsonSerializerSettings prettyJson = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented
};
public class dialup {
public Dictionary<string,uint> speeds;
public string phonenumber;
}
public class Ethernet {
public string speed;
}
public class ipv4 {
public bool somecapability;
}
public class SiteData {
public string SiteName;
[JsonExtensionData]
public Dictionary<string, object> ConnectionTypes;
}
public static void Main()
{
var data = new SiteData()
{
SiteName = "Foo",
ConnectionTypes = new Dictionary<string, object>()
{
{ "1", new dialup() { speeds=new Dictionary<string,uint>() {{"1",9600},{"2",115200}}, phonenumber = "0118 999 881 999 119 725 ... 3" } },
{ "2", new Ethernet() { speed = "1000" } },
{"3", new ipv4() { somecapability=true}}
}
};
var json = JsonConvert.SerializeObject(data, prettyJson);
Console.WriteLine(json);
}
}
This results in the following JSON:
{
"siteName": "Foo",
"1": {
"speeds": {
"1": 9600,
"2": 115200
},
"phonenumber": "0118 999 881 999 119 725 ... 3"
},
"2": {
"speed": "1000"
},
"3": {
"somecapability": true
}
}
What I need in the JSON is:
{
"siteName": "Foo",
"1": {
"dialup":{
"speeds": {
"1": 9600,
"2": 115200
},
"phonenumber": "0118 999 881 999 119 725 ... 3"
}
},
"2": {
"Ethernet":{
"speed": "1000"
}
},
"3": {
"ipv4":{
"somecapability": true
}
}
}
How can I do this using Json.NET? Json.NET deserializes this just fine, but I've been looking for days on how to make it serialize it the same way.
{ }whereas properties (e.gspeed) get the name serialized