不能使用Json.net使用复杂键序列化字典 [英] Not ableTo Serialize Dictionary with Complex key using 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\:\正常\
This Give me --> "{\"JSonSerialization.ListBaseClass\":\"Normal\"}"
但是,如果我将自定义类型作为字典中的值,那么 b
$ b
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);
这给我 - >{\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?
推荐答案
序列化指南(参见:字典和Hashtables部分;谢谢@Shashwat的链接):
The Serialization Guide states (see section: Dictionaries and Hashtables; thank you @Shashwat for the link):
当序列化字典时,字典的键将
转换为字符串,并用作JSON对象属性名称。为密钥编写的
字符串可以通过重写
ToString()作为键类型或通过实现TypeConverter进行自定义。 A
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的how-to页面上实现此类型转换器的有用示例:
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)
对象ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture ,对象值)
对象ConvertTo(ITypeDescriptorContext上下文,
System.Globalization.CultureInfo文化,
对象值,类型destinationType)
还需要将 [TypeConverter(typeof(MyClassConverter))]
的属性添加到
有了这些,我可以自动序列化和反序列化字典 / em>。
With these in place, I was able to serialize and deserialize dictionaries automatically.
这篇关于不能使用Json.net使用复杂键序列化字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!