保持套管序列化时字典 [英] Keep casing when serializing dictionaries

查看:181
本文介绍了保持套管序列化时字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API项目被配置是这样的:

  config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =新CamelCasePropertyNamesContractResolver();

不过,我想字典键套管保持不变。有没有在 Newtonsoft.Json 任何属性,我可以使用一个类来表示,我想套管序列化过程中保持不变?

 公共类SomeViewModel
{
    公共字典<字符串,字符串>数据{搞定;组; }
}


解决方案

有没有做到这一点的属性,但你可以通过自定义解析器做到这一点。

我看到你已经在使用 CamelCasePropertyNamesContractResolver 。如果你获得从一个新的解析器类并重写 CreateDictionaryContract()方法,可以提供替代 DictionaryKeyResolver 功能不改变键名。

下面是code,你将需要:

 类CamelCaseExceptDictionaryKeysResolver:CamelCasePropertyNamesContractResolver
{
    保护覆盖JsonDictionaryContract CreateDictionaryContract(类型的objectType)
    {
        JsonDictionaryContract合同= base.CreateDictionaryContract(的objectType);        contract.DictionaryKeyResolver = propertyName的= GT; propertyName的;        返回合同;
    }
}

演示:

 类节目
{
    静态无效的主要(字串[] args)
    {
        富富=新富
        {
            AnIntegerProperty = 42,
            HTMLString =< HTML和GT;< / HTML>中,
            字典=新字典<字符串,字符串>
            {
                {whizbang的,1},
                {FOO,2},
                {酒吧,3},
            }
        };        JsonSerializerSettings设置=新JsonSerializerSettings
        {
            ContractResolver =新CamelCaseExceptDictionaryKeysResolver()
            格式化= Formatting.Indented
        };        JSON字符串= JsonConvert.SerializeObject(foo,那么设置);
        Console.WriteLine(JSON);
    }
}Foo类
{
    公众诠释AnIntegerProperty {搞定;组; }
    公共字符串HTMLString {搞定;组; }
    公共字典<字符串,字符串>字典{搞定;组; }
}

下面是从上述的输出。请注意,所有的类属性名称的骆驼套管,但字典键都保留了其原有的外壳。

  {
  anIntegerProperty:42,
  htmlString:< HTML和GT;< / HTML>中,
  说文解字:{
    whizbang的:1,
    富:2
    吧:3
  }
}

I have a Web Api project being configured like this:

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

However, I want dictionary keys casing to remain unchanged. is there any attribute in Newtonsoft.Json I can use to a class to denote that I want casing to remain unchanged during serialization?

public class SomeViewModel
{
    public Dictionary<string, string> Data { get; set; }    
}

解决方案

There is not an attribute to do this, but you can do it by customizing the resolver.

I see that you are already using a CamelCasePropertyNamesContractResolver. If you derive a new resolver class from that and override the CreateDictionaryContract() method, you can provide a substitute DictionaryKeyResolver function that does not change the key names.

Here is the code you would need:

class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver
{
    protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
    {
        JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);

        contract.DictionaryKeyResolver = propertyName => propertyName;

        return contract;
    }
}

Demo:

class Program
{
    static void Main(string[] args)
    {
        Foo foo = new Foo
        {
            AnIntegerProperty = 42,
            HTMLString = "<html></html>",
            Dictionary = new Dictionary<string, string>
            {
                { "WHIZbang", "1" },
                { "FOO", "2" },
                { "Bar", "3" },
            }
        };

        JsonSerializerSettings settings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCaseExceptDictionaryKeysResolver(),
            Formatting = Formatting.Indented
        };

        string json = JsonConvert.SerializeObject(foo, settings);
        Console.WriteLine(json);
    }
}

class Foo
{
    public int AnIntegerProperty { get; set; }
    public string HTMLString { get; set; }
    public Dictionary<string, string> Dictionary { get; set; }
}

Here is the output from the above. Notice that all of the class property names are camel-cased, but the dictionary keys have retained their original casing.

{
  "anIntegerProperty": 42,
  "htmlString": "<html></html>",
  "dictionary": {
    "WHIZbang": "1",
    "FOO": "2",
    "Bar": "3"
  }
}

这篇关于保持套管序列化时字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆