的ISet< T>序列化使用DataContractJsonSerializer JSON序列化、LT、ISet、GT

2023-09-05 01:33:45 作者:你可爱的野爹

我做了一些测试,以检查/理解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

推荐答案

看起来这是一个 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 的接口,它应该是一个支持的接口,或用水泥的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.