使用具有IDictionary <Mycustomclass,List <string >>的自定义解析器进行反序列化问题

MSTdev

问题:

我有一个Foo包含的课程IDictionary<MyCustomClass, List<string>>我正在使用此答案中ContractResolver所示的自定义以便我的字典将序列化为包含“键”和“值”属性的对象数组。那部分工作正常。但是,当我尝试使用相同的解析器反序列化JSON时,出现如下所示的错误。Foo

错误:

运行时异常(第46行):无法将当前JSON数组(例如[1,2,3])反序列化为类型'System.Collections.Generic.IDictionary 2[mycustomclass,System.Collections.Generic.List1 [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"
      ]
    }
  ]
}

有人可以帮助我让它正常工作吗?

Artiom

不知道这是否是最好的方法,但是可以解决您的问题。创建一个转换器

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()}
    };

请享用。

样品

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用自定义合同解析器而不是JsonConverter属性时,将忽略自定义JsonConverter进行反序列化

如何将`Dictionary <string,Dictionary <string,List <MyCustomClass >>>`转换为`Dictionary <string,List <MyCustomClass >>>>

使用Jackson进行自定义反序列化:扩展默认反序列化器

属性具有IDictionary <string,string>时反序列化对象

如何使Json.NET序列化和反序列化还实现IDictionary <string,object>的自定义动态类型的声明属性?

用于序列化List <String>中的List <User>的自定义序列化程序

如何使用Java对自定义日期反序列化器进行单元测试

使用具有对象结构的键序列化 Dictionary<string, string>

对接口集合进行反序列化时,使用自定义转换器对以前的数据模型进行JSON的反序列化失败

Gson自定义反序列化器,用于String类

如何使用Json.Net使用自定义键对字典进行序列化/反序列化?

使用自定义elementName将xml反序列化为List <T>

如何使用具有标量值的类构建一个List <string>?

使用Serde对数组或值数组进行自定义反序列化

在Java中使用Jackson进行JSON FIELD的自定义反序列化?

在PyYAML中使用自定义类密钥对字典进行反序列化失败

使用System.Text.Json进行自定义反序列化

使用Json.NET为具有多个值类型的字段自定义反序列化器

使用自定义IXmlSerializer反序列化注释

使用GSON自定义反序列化枚举

使用Jackson自定义JSON反序列化

使用Jackson自定义反序列化列表

使用GSON使用自定义比较器反序列化扩展TreeSet的类

fastxml使用toString进行序列化,并使用String构造函数进行反序列化

如何在Spring @RequestParam中使用自定义反序列化器

使用Gson的自定义JSON反序列化器

Spring Boot对ZonedDateTime不使用自定义反序列化器

使用自定义转换器从流中反序列化Json

使用具有$符号的变量对JSON进行反序列化