T>实施了IEnumerable&LT自定义集合的Json.net系列化; [英] Json.net serialization of custom collection implementing IEnumerable<T>

查看:114
本文介绍了T>实施了IEnumerable&LT自定义集合的Json.net系列化;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现IEnumerable的集合类,我有麻烦了反序列化相同的序列化版本。我使用Json.net v 4.0.2.13623

I have a collection class that implements IEnumerable and I am having trouble deserializing a serialized version of the same. I am using Json.net v 4.0.2.13623

下面是我收集类的simplier版本,说明我的问题。

Here is a simplier version of my collection class that illustrates my issue

public class MyType
{
  public int Number { get; private set; }
  public MyType(int number)
  {
    this.Number = number;
  }
}

public class MyTypes : IEnumerable<MyType>
{
  private readonly Dictionary<int, MyType> c_index;
  public MyTypes(IEnumerable<MyType> seed)
  {
    this.c_index = seed.ToDictionary(item => item.Number);
  }
  public IEnumerator<MyType> GetEnumerator()
  {
    return this.c_index.Values.GetEnumerator();
  }
  IEnumerator IEnumerable.GetEnumerator()
  {
    return this.GetEnumerator();
  }
  public int CustomMethod()
  {
    return 1;  // Just for illustration
  }
}

当我序列化,我得到下面的JSON

When I serialize it i get the following json

[
  {
    "Number": 1
  },
  {
    "Number": 2
  }
]

但后来当我尝试反序列化我得到下面的异常
的System.Exception:无法创建并填充列表类型MyTypes

But then when i try to deserialize i get the following exception System.Exception: Cannot create and populate list type MyTypes

使用序列提示曾经也试过,但没有成功

Have also tried using serialization hints but have had no success

这是测试的功能,我使用了

These are the test function i have used

public void RoundTrip()
{
  var _myTypes1 = new MyTypes(new[] { new MyType(1), new MyType(2) });
  var _jsonContent = JsonConvert.SerializeObject(_myTypes1, Formatting.Indented);
  var _myTypes2 = JsonConvert.DeserializeObject<MyTypes>(_jsonContent);
}
public void RoundTripWithSettings()
{
  var _myTypes1 = new MyTypes(new[] { new MyType(1), new MyType(2) });

  var _serializationSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Arrays };
  var _jsonContent = JsonConvert.SerializeObject(_myTypes1, Formatting.Indented, _serializationSettings);
  var _myTypes2 = JsonConvert.DeserializeObject<MyTypes>(_jsonContent, _serializationSettings);
}



有没有人管理序列化自己的收藏对象?

Has anybody managed to serialize their own collection objects ?

在此先感谢

帕特

推荐答案

由于Json.NET 6.0第3版及更高版本(我在8.0.3测试),这种自动只要工作作为实现自定义类的IEnumerable< MyType的> 有一个构造函数的的IEnumerable<&MyType的GT; 输入参数。从的发行说明

As of Json.NET 6.0 Release 3 and later (I tested on 8.0.3), this works automatically as long as the custom class implementing IEnumerable<MyType> has a constructor that takes an IEnumerable<MyType> input argument. From the release notes:

要一成不变.NET集合所有未来的创造者:如果你的T收集有一个构造函数IEnumerable的反序列化,然后到您的收藏时,Json.NET会自动工作,否则你所有的运气了。

To all future creators of immutable .NET collections: If your collection of T has a constructor that takes IEnumerable then Json.NET will automatically work when deserializing to your collection, otherwise you're all out of luck.

由于 MyTypes 在问题类有这样一个构造函数,这个现在的只是工作的。示范小提琴: https://dotnetfiddle.net/LRJHbd

Since the MyTypes class in the question has just such a constructor, this now just works. Demonstration fiddle: https://dotnetfiddle.net/LRJHbd.

没有这样的构造,一会需要添加一个参数的构造函数和落实的ICollection< MyType的> (而不是仅仅的IEnumerable< MyType的> )与工作添加(的MyType项目)方法。然后Json.NET可以构造和填充集合。或反序列化到中间集合作为原来的答案

Absent such a constructor, one would need to add a parameterless constructor and implement ICollection<MyType> (instead of just IEnumerable<MyType>) with a working Add(MyType item) method. Json.NET can then construct and populate the collection. Or deserialize to an intermediate collection as in the original answer.

这篇关于T&GT;实施了IEnumerable&LT自定义集合的Json.net系列化;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆