无法使用 Json.net 使用复杂键序列化字典 [英] Not ableTo Serialize Dictionary with Complex key using Json.net

查看:29
本文介绍了无法使用 Json.net 使用复杂键序列化字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以自定义 .net 类型为键的字典.我正在尝试使用 JSON.net 将此字典序列化为 JSON,但是它无法在序列化期间将键转换为正确的值.

I have a dictionary with a custom .net Type as Its key.I am trying to serialize this dictionary to JSON using JSON.net, However its not able to Convert Keys to Proper Value during Serialization.

class ListBaseClass
{
    public String testA;
    public String testB;
}
-----
var details = new Dictionary<ListBaseClass, string>();
details.Add(new ListBaseClass { testA = "Hello", testB = "World" }, "Normal");
var results = Newtonsoft.Json.JsonConvert.SerializeObject(details);
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<ListBaseClass, string>> results);

这给我 --> "{"JSonSerialization.ListBaseClass":"Normal"}"

This Give me --> "{"JSonSerialization.ListBaseClass":"Normal"}"

但是,如果我将自定义类型作为字典中的值,它运行良好

However if I have my Custom type as value in Dictionary it Works well

  var details = new Dictionary<string, ListBaseClass>();
  details.Add("Normal", new ListBaseClass { testA = "Hello", testB = "World" });
  var results = Newtonsoft.Json.JsonConvert.SerializeObject(details);
  var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, ListBaseClass>>(results);

This Give me --> "{"Normal":{"testA":"Hello","testB":"World"}}"

This Give me --> "{"Normal":{"testA":"Hello","testB":"World"}}"

如果我遇到了 Json.net 的某些限制或者我做错了什么,有人可以提出建议吗?

Can Someone Suggest If I am hitting some limitation of Json.net or I am doing Something Wrong?

推荐答案

序列化指南 状态(请参阅部分:字典和哈希表;感谢@Shashwat 提供链接):

The Serialization Guide states (see section: Dictionaries and Hashtables; thank you @Shashwat for the link):

序列化字典时,字典的键是转换为字符串并用作 JSON 对象属性名称.这可以通过覆盖来自定义为键编写的字符串ToString() 用于键类型或通过实现 TypeConverter.一个TypeConverter 还将支持再次转换自定义字符串反序列化字典时.

When serializing a dictionary, the keys of the dictionary are converted to strings and used as the JSON object property names. The string written for a key can be customized by either overriding ToString() for the key type or by implementing a TypeConverter. A TypeConverter will also support converting a custom string back again when deserializing a dictionary.

我在 Microsoft 的操作方法"页面上找到了一个关于如何实现此类类型转换器的有用示例:

I found a useful example for how to implement such a type converter on Microsoft's "how-to" page:

  • Implement a Type Converter (see section Type Converters for Value Translation).

本质上,我需要扩展 System.ComponentModel.TypeConverter 并覆盖:

Essentially, I needed to extend System.ComponentModel.TypeConverter and override:

bool CanConvertFrom(ITypeDescriptorContext context, Type source);

object ConvertFrom(ITypeDescriptorContext context,
                   System.Globalization.CultureInfo culture, object value);

object ConvertTo(ITypeDescriptorContext context, 
                 System.Globalization.CultureInfo culture, 
                 object value, Type destinationType);

还需要在 MyClass 类声明中添加属性 [TypeConverter(typeof(MyClassConverter))].

It was also necessary to add the attribute [TypeConverter(typeof(MyClassConverter))] to the MyClass class declaration.

有了这些,我就能够自动序列化和反序列化字典.

With these in place, I was able to serialize and deserialize dictionaries automatically.

这篇关于无法使用 Json.net 使用复杂键序列化字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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