问题
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