问题:
我的课程Foo
包含IDictionary<MyCustomClass, List<string>>
。我正在使用this answer中显示的自定义ContractResolver
,以便我的字典将序列化为包含“Key”和“Value”属性的对象数组。那部分工作正常。但是,当我尝试使用相同的解析器将JSON反序列化为Foo
时,出现如下所示的错误。
错误:
运行时异常(第46行):无法将当前JSON数组(例如[1,2,3])反序列化为类型 'System.Collections.Generic.IDictionary
2[mycustomclass,System.Collections.Generic.List
1 [System.String]]' 因为类型需要JSON对象(例如{"name":"value"}
) 正确地反序列化。要修复此错误,请将JSON更改为JSON对象(例如
{"name":"value"}
)或将反序列化类型更改为数组或 实现集合接口的类型(例如ICollection,IList) 像可以从JSON数组反序列化的List。 JsonArrayAttribute也可以添加到类型中以强制它 从JSON数组反序列化。路径'Dict2',第12行,第13位。
Dotnetfiddle:
https://dotnetfiddle.net/BKaKab
我的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class mycustomclass
{
public string name {get;set;}
public List<string> Productlist {get;set;}
public List<string> SelectedItems{get;set;}
}
public class Program
{
public static void Main()
{
mycustomclass omycustomclass = new mycustomclass{name = "Test",Productlist = new string[]{"Product1","Product2","Product3"}.ToList(), SelectedItems = new string[]{"Item1","Item2","Item3"}.ToList()};
mycustomclass omycustomclass2 = new mycustomclass{name = "Test",Productlist = new string[]{"Product4","Product5","Product6"}.ToList(), SelectedItems = new string[]{"Item4","Item5","Item6"}.ToList()};
Foo foo = new Foo();
foo.Dict = new Dictionary<string, string>();
foo.Dict.Add("Gee", "Whiz");
foo.Dict.Add("Fizz", "Bang");
Dictionary<mycustomclass, List<string>> custom = new Dictionary<mycustomclass, List<string>>();
custom.Add(omycustomclass, new string[]{"l1","l2","l3"}.ToList());
custom.Add(omycustomclass2, new string[]{"l4","l5","l6"}.ToList());
foo.Dict2 = custom;
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new DictionaryAsArrayResolver();
// serialize
string json = JsonConvert.SerializeObject(foo, settings);
Console.WriteLine(json);
Console.WriteLine();
// deserialize
foo = JsonConvert.DeserializeObject<Foo>(json, settings);
foreach (var kvp in foo.Dict)
{
Console.WriteLine(kvp.Key + ": " + kvp.Value);
}
}
class Foo
{
public Dictionary<string, string> Dict { get; set; }
public IDictionary<mycustomclass, List<string>> Dict2 { get; set; }
}
}
class DictionaryAsArrayResolver : DefaultContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
if (objectType.GetInterfaces().Any(i => i == typeof(IDictionary) ||
(i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))))
{
return base.CreateArrayContract(objectType);
}
return base.CreateContract(objectType);
}
}
JSON:
{
"Dict": [
{
"Key": "Gee",
"Value": "Whiz"
},
{
"Key": "Fizz",
"Value": "Bang"
}
],
"Dict2": [
{
"Key": {
"name": "Test",
"Productlist": [
"Product1",
"Product2",
"Product3"
],
"SelectedItems": [
"Item1",
"Item2",
"Item3"
]
},
"Value": [
"l1",
"l2",
"l3"
]
},
{
"Key": {
"name": "Test",
"Productlist": [
"Product1",
"Product2",
"Product3"
],
"SelectedItems": [
"Item1",
"Item2",
"Item3"
]
},
"Value": [
"l1",
"l2",
"l3"
]
}
]
}
有人可以帮我搞定吗?
答案 0 :(得分:3)
不确定它是否是最佳方式,但它可以解决您的问题。 创建转换器
internal class MyConverter : CustomCreationConverter<IDictionary<CustomClass, List<String>>>
{
public override IDictionary<CustomClass, List<String>> Create(Type objectType)
{
return new Dictionary<CustomClass, List<String>>();
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof (object) || base.CanConvert(objectType);
}
}
将其添加到您的设置中:
var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new DictionaryAsArrayResolver(),
Converters = new JsonConverter[] {new MyConverter()}
};
享受。
答案 1 :(得分:0)
一种更干净的方法是通过设置JsonArrayContract.OverrideCreator来定义创建方法:
public class DictionaryAsArrayResolver : DefaultContractResolver
{
public override JsonContract ResolveContract(Type objectType)
{
if (IsDictionary(objectType))
{
JsonArrayContract contract = base.CreateArrayContract(objectType);
contract.OverrideCreator = (args) => CreateInstance(objectType);
return contract;
}
return base.CreateContract(objectType);
}
internal static bool IsDictionary(Type objectType)
{
if (objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(IDictionary<,>))
{
return true;
}
if (objectType.GetInterface(typeof(IDictionary<,>).Name) != null)
{
return true;
}
return false;
}
private object CreateInstance(Type objectType)
{
Type dictionaryType = typeof(Dictionary<,>).MakeGenericType(objectType.GetGenericArguments());
return Activator.CreateInstance(dictionaryType);
}
}