枚举的Int32>使用JSON序列化字典<的问题; [英] Problems with Json Serialize Dictionary<Enum, Int32>
问题描述
结果
每当我试图序列化字典我得到异常:
whenever i try to serialize the dictionary i get the exception:
System.ArgumentException: Type
'System.Collections.Generic.Dictionary`2[[Foo.DictionarySerializationTest+TestEnum, Foo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'
is not supported for serialization/deserialization of a dictionary,
keys must be strings or object
我的测试用例是:
public class DictionarySerializationTest
{
public enum TestEnum { A, B, C }
//tried with numbers, too: public enum TestEnum { A = 1, B = 2, C = 3 }
public void SerializationTest()
{
Dictionary<TestEnum, Int32> data = new Dictionary<TestEnum, Int32>();
data.Add(TestEnum.A, 1);
data.Add(TestEnum.B, 2);
data.Add(TestEnum.C, 3);
JavaScriptSerializer serializer = new JavaScriptSerializer();
String result = serializer.Serialize(data);
// Throws
}
public void SerializationToObjectTest()
{
Dictionary<object, Int32> data = new Dictionary<object, Int32>();
data.Add(Enum.ToObject(typeof(TestEnum), TestEnum.A), 1);
data.Add(Enum.ToObject(typeof(TestEnum), TestEnum.B), 2);
data.Add(Enum.ToObject(typeof(TestEnum), TestEnum.C), 3);
JavaScriptSerializer serializer = new JavaScriptSerializer();
String result = serializer.Serialize(data);
// Throws
}
public void SerializationStringTest()
{
Dictionary<String, Int32> data = new Dictionary<String, Int32>();
data.Add(TestEnum.A.ToString(), 1);
data.Add(TestEnum.B.ToString(), 2);
data.Add(TestEnum.C.ToString(), 3);
JavaScriptSerializer serializer = new JavaScriptSerializer();
String result = serializer.Serialize(data);
// Succeeds
}
}
当然,我可以使用的ToString()无论何时我进入的东西进入词典,但因为它的使用往往在性能上的相关方法我宁愿用枚举。
Of course i could use .ToString() whenever i enter something into the Dictionary but since it's used quite often in performance relevant methods i would prefer using the enum.
我的唯一解决方案是使用的ToString(),然后输入性能关键区域之前转换但是这是笨拙的,我不得不改变我的代码结构只是为了能够序列化的数据。
My only solution is using .ToString() and converting before entering the performance critical regions but that is clumsy and i would have to change my code structure just to be able to serialize the data.
有没有人有一个想法如何,我可以序列化字典为<枚举的Int32>
Does anyone have an idea how i could serialize the dictionary as <Enum, Int32>
?
我。使用System.Web.Script.Serialization.JavaScriptSerializer系列化
I use the System.Web.Script.Serialization.JavaScriptSerializer for serialization.
更新:
我切换到词典<字符串的Int32>
现在和它的作品,但我希望有人给出一个解决方案,因为我真的不喜欢到位的类型使用字符串安全枚举。
I switched to Dictionary<String, Int32>
now and it works but i hope someone shows a solution as i don't really like using strings in place of a type safe enum.
推荐答案
我知道它的晚,但也许别人可以在将来使用它。你可以达到你需要使用LINQ是什么:
I know it's late, but maybe someone else can make use of it in the future. You can achieve what you need using LINQ:
Dictionary<TestEnum, Int32> data = new Dictionary<TestEnum, Int32>();
data.Add(TestEnum.A, 1);
data.Add(TestEnum.B, 2);
data.Add(TestEnum.C, 3);
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, Int32> dataToSerialize = data.Keys.ToDictionary(p => p.ToString(), p => data[p]);
string dataSerialized = serializer.Serialize(dataToSerialize);
这篇关于枚举的Int32>使用JSON序列化字典<的问题;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!