简体   繁体   English

在Json.Net中序列化类名

[英]Serialize class name in Json.Net

I have the following C# code: 我有以下C#代码:

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: 这将导致以下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: 我在JSON中需要的是:

{
  "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做到这一点? Json.NET deserializes this just fine, but I've been looking for days on how to make it serialize it the same way. Json.NET反序列化就好了,但是我一直在寻找如何以相同的方式对其进行序列化的日子。

To accomplish this, you need to wrap each of the values in your ConnectionTypes dictionary in another dictionary. 为此,您需要将ConnectionTypes词典中的每个值包装在另一个词典中。 You can create a helper method to make this easier: 您可以创建一个辅助方法来简化此操作:

private static Dictionary<string, object> WrapInDictionary(object value) 
{
    return new Dictionary<string, object>()
    {
        { value.GetType().Name, value }
    };
}

Then you can initialize your data like this: 然后,您可以像这样初始化数据:

var data = new SiteData()
{
    SiteName = "Foo",
    ConnectionTypes = new Dictionary<string, object>() 
    {
        { "1", WrapInDictionary( new dialup() { Speeds = new Dictionary<string, uint>() { {"1", 9600}, {"2", 115200} }, PhoneNumber = "0118 999 881 999 119 725 ... 3" } ) },
        { "2", WrapInDictionary( new Ethernet() { Speed = "1000" } ) },
        { "3", WrapInDictionary( new ipv4() { SomeCapability=true } ) }
    }
};

Fiddle: https://dotnetfiddle.net/gGXlDo 小提琴: https : //dotnetfiddle.net/gGXlDo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM