序列化和反序列化字典< int,object>使用JavaScriptSerializer和自定义JavaScriptConverter [英] Serialize and deserialize Dictionary<int, object> using JavaScriptSerializer and a custom JavaScriptConverter
问题描述
如何使用整数 的字典序列化和反序列化为JSON -us / library / system.web.script.serialization.javascriptserializer%28v = vs.110%29.aspxrel =nofollow noreferrer> JavaScriptSerializer 和一个自定义 JavaScriptConverter ?
How would you serialize and deserialize a dictionary whose keys are integers to JSON, using a JavaScriptSerializer and a custom JavaScriptConverter?
对于那些不知道的人,JavaScripSerializer不能开箱即用。
For those who don't know, the JavaScripSerializer cannot do this out of the box.
请注意,我不感兴趣一个解决方案,需要在序列化之前转换字典,或者使用另一个序列化程序(如果是,您可能会看到这篇文章)。
Please note that I'm not interested in a solution that requires converting the dictionary prior to serialization or that uses another serializer (if you are, you may see this post).
更新:删除任何歧义,我没有问题的事实关键在JSON中生成为一个字符串(因此1将变为1)。
UPDATE: to remove any ambiguity, I have no problem with the fact the key is generated as a string in JSON (so 1 will become "1").
推荐答案
/// <summary>
/// Implements JavaScript Serialization and Deserialization for instances of the Dictionary<int, object> type.
/// </summary>
public class IntDictionaryConverter : JavaScriptConverter
{
/// <summary>
/// Converts the provided dictionary into a Dictionary<int, object> object.
/// </summary>
/// <param name="dictionary">An IDictionary instance of property data stored as name/value pairs.</param>
/// <param name="type">The type of the resulting object.</param>
/// <param name="serializer">The JavaScriptSerializer instance.</param>
/// <returns>The deserialized object.</returns>
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
// Validate arguments
if (dictionary == null) throw new ArgumentNullException("dictionary");
if (serializer == null) throw new ArgumentNullException("serializer");
Dictionary<int, object> deserializedDictionary = new Dictionary<int, object>();
foreach (KeyValuePair<string, object> entry in dictionary)
{
int intKey = 0;
if (!int.TryParse(entry.Key, out intKey))
throw new InvalidOperationException("Cannot deserialize the dictionary because of invalid number string");
deserializedDictionary.Add(intKey, entry.Value);
}
return deserializedDictionary;
}
/// <summary>
/// Builds a dictionary of name/value pairs.
/// </summary>
/// <param name="obj">The object to serialize.</param>
/// <param name="serializer">The object that is responsible for the serialization.</param>
/// <returns>An object that contains key/value pairs that represent the object’s data.</returns>
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
// Validate arguments
if (obj == null) throw new ArgumentNullException("obj");
if (serializer == null) throw new ArgumentNullException("serializer");
// Get the dictionary to convert
Dictionary<int, object> dictionary = (Dictionary<int, object>)obj;
// Build the converted dictionary
Dictionary<string, object> convertedDictionary = new Dictionary<string, object>();
foreach (KeyValuePair<int, object> entry in dictionary)
convertedDictionary.Add(entry.Key.ToString(), entry.Value);
return convertedDictionary;
}
/// <summary>
/// Gets a collection of the supported types.
/// </summary>
public override System.Collections.Generic.IEnumerable<Type> SupportedTypes
{
get
{
return new Type[]
{
typeof(Dictionary<int, object>)
};
}
}
}
这篇关于序列化和反序列化字典< int,object>使用JavaScriptSerializer和自定义JavaScriptConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!