的ISet< T>序列化使用DataContractJsonSerializer JSON [英] ISet<T> serialization to JSON using DataContractJsonSerializer

查看:124
本文介绍了的ISet< T>序列化使用DataContractJsonSerializer JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些测试,以检查/理解JSON序列化到/从.NET类型在C#。 我试图用DataContractJsonSerializer。

I am doing some tests to check/understand JSON serialization to/from .Net types in C#. I am trying to use the DataContractJsonSerializer.

下面是我想序列化样品类型:

Here is a sample type that I am trying to serialize:

[DataContract]
[KnownType(typeof(HashSet<int>))]
public class TestModel
{
    [DataMember]
    public string StreetName { get; private set; }
    [DataMember]
    public int StreetId { get; private set; }
    [DataMember]
    public int NumberOfCars { get; set; }
    [DataMember]
    public IDictionary<string, string> HouseDetails { get; set; }
    [DataMember]
    public IDictionary<int, string> People { get; set; }
    [DataMember]
    public ISet<int> LampPosts { get; set; }

    public TestModel(int StreetId, string StreetName)
    {
        this.StreetName = StreetName;
        this.StreetId = StreetId;

        HouseDetails = new Dictionary<string, string>();
        People = new Dictionary<int, string>();
        LampPosts = new HashSet<int>();
    }

    public void AddHouse(string HouseNumber, string HouseName)
    {
        HouseDetails.Add(HouseNumber, HouseName);
    }

    public void AddPeople(int PersonNumber, string PersonName)
    {
        People.Add(PersonNumber, PersonName);
    }

    public void AddLampPost(int LampPostName)
    {
        LampPosts.Add(LampPostName);
    }
}

当我再尝试序列这种类型的使用DataContractJsonSerializer一个对象,我得到以下错误:

When I then try to serialize an object of this type using DataContractJsonSerializer, I get the following error:

{"'System.Collections.Generic.HashSet`1[System.Int32]' is a collection type and cannot be serialized when assigned to an interface type that does not implement IEnumerable ('System.Collections.Generic.ISet`1[System.Int32]'.)"}

这条消息听起来是不是我的权利。 的ISet&LT; T&GT; 并实施的IEnumerable&LT; T&GT; (也IEnumerable的)。 如果我TestModel课,我替换

This msg does not sound right to me. ISet<T> does implement IEnumerable<T>( and also IEnumerable). If in my TestModel class, I replace

public ISet<int> LampPosts { get; set; }

public ICollection<int> LampPosts { get; set; }...

则全部通过帆。

then it all sails through.

我是新来的JSON所以任何帮助将是pciated大大AP $ P $

I am new to JSON so any help would be greatly appreciated

推荐答案

看起来这是一个<一个href="https://connect.microsoft.com/VisualStudio/feedbackdetail/view/797718/datacontractserializer-cannot-serialize-system-collections-generic-iset-t-even-if-it-derives-from-ienumerable"相对=nofollow> Microsoft已知的bug 。 支持的接口列表是很难codeD的框架,而的ISet 是不是其中之一:

Looks like this is a known microsoft bug. List of supported interfaces is hardcoded in framework, and ISet is not one of them:

CollectionDataContract.CollectionDataContractCriticalHelper._knownInterfaces = new Type[]
{
  Globals.TypeOfIDictionaryGeneric,
  Globals.TypeOfIDictionary,
  Globals.TypeOfIListGeneric,
  Globals.TypeOfICollectionGeneric,
  Globals.TypeOfIList,
  Globals.TypeOfIEnumerableGeneric,
  Globals.TypeOfICollection,
  Globals.TypeOfIEnumerable
};

是的,错误信息是不正确。 因此, DataContractJsonSerializer 不能序列的ISet 的接口,它应该是一个支持的接口,或用水泥<替换code>的ISet 的实施。

And yes, error message is incorrect. So, DataContractJsonSerializer cannot serialize ISet interface, it should be either replaced with one of supported interfaces, or with concrete ISet implementation.

这篇关于的ISet&LT; T&GT;序列化使用DataContractJsonSerializer JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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