JSON.NET cannot deserialize a wrapped collection

↘锁芯ラ 提交于 2020-01-02 10:29:11

问题


I have a wrapped list that looks like this:

[Serializable]
public class OrderManager : IEnumerable<Order>
{
    public OrderManager()
    { }

    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        orders.Add(order);
    }

    public IEnumerator<Order> GetEnumerator()
    {
        return orders.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return orders.GetEnumerator();
    }
}

I include a field instance in a customer class like this:

[Serializable]
public class Customer
{
    public Customer()
    { }

    private OrderManager _orders
        = new OrderManager();
    public OrderManager Orders
    {
        get { return _orders; }
        set { _orders = value; }
    }
}

I create the JSON.NET serializer like this:

private JsonSerializer GetSerializer()
{
    var serializer = new JsonSerializer();

    // on or off the type name handling doesn't help
    //serializer.TypeNameHandling = TypeNameHandling.All;

    var contractResolver = new DefaultContractResolver(true);
    contractResolver.IgnoreSerializableAttribute = false;
    contractResolver.IgnoreSerializableInterface = false;

    serializer.ContractResolver = contractResolver;

    serializer.PreserveReferencesHandling = PreserveReferencesHandling.All;
    serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

    return serializer;          
}

Serialization of a customer object works, but during deserialization I get the error:

Error setting value to '_orders' on 'Tests.SerializationTests+Customer'.

With TypeNameHandling set to All I get this:

Type specified in JSON 'Tests.SerializationTests+Order[], Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with 'Tests.SerializationTests+OrderManager, Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Path '_orders.$type', line 1, position 232.

Any idea how to get Json.NET to play nice with the wrapped list?


回答1:


I guess you need to implement ICollection<Order>

public class OrderManager : IEnumerable<Order>,ICollection<Order>

-

[Serializable]
public class OrderManager : IEnumerable<Order>,ICollection<Order>
{
    public OrderManager()
    { }

    private List<Order> orders = new List<Order>();

    public Order this[int i]
    {
        set { AddOrder(value); }
        get { return orders[i]; }
    }

    public void AddOrder(Order order)
    {
        orders.Add(order);
    }

    public IEnumerator<Order> GetEnumerator()
    {
        return orders.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return orders.GetEnumerator();
    }

    public void Add(Order item)
    {
        AddOrder(item);
    }

    public void Clear()
    {
        orders.Clear();
    }

    public bool Contains(Order item)
    {
        return orders.Contains(item);
    }

    public void CopyTo(Order[] array, int arrayIndex)
    {
        throw new NotImplementedException();
    }

    public int Count
    {
        get { return orders.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(Order item)
    {
        return orders.Remove(item);
    }
}



回答2:


Try one of these two things:

Make a wrapper collection class around your List called OrderCollection:

[Serializable]/[DataContract]
public OrderCollection : List<Order> { ... }

Or try turning the LIST into an ARRAY.

Let us know how either one works.




回答3:


I am not sure serialization works with IEnumerable, could you try it with a List?

[Serializable]
public class OrderManager : List<Order>


来源:https://stackoverflow.com/questions/10488351/json-net-cannot-deserialize-a-wrapped-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!