failure to convert parameter when serializing

Hi all, I have a wcf service that is sending my unity client some data and all was going fine until I extended it slightly and now there seems to be a serialization problem. I’ll give an example of what’s going on, its not my code but is laid out the exact same way.

[DataContract]
public class Response
{
    [DataMember]
    public bool valid = true;
    [DataMember]
    public string errorMsg;
    [DataMember]
    public List<Customer> customers;
}

[DataContract]
public class Customer
{
    [DataMember]
    public string name;
    [DataMember]
    public ShippingAddress address1;
    [DataMember]
    public ShippingAddress address2;
}

[DataContract]
public class ShippingAddress
{
    [DataMember]
    public string address;
    [DataMember]
    public List<Order> orders;
}

[DataContract]
public class Order
{
    [DataMember]
    public string foo;
    [DataMember]
    public List<Bar> bars;
}

[DataContract]
public class Bar
{
    [DataMember]
    public float thingy;
}

Now before I made what would be the ‘Order’ and ‘Bar’ Classes it all worked fine. It serialized my list of customers in the response class fine and everything. Now though as I have added the ‘Order’ and ‘Bar’ Classes it throws an error serializing the list of orders in the ‘ShippingAddress’ class. I’ve read that this might be to do with serializing a list within a list, any ideas on how to solve this or ways to work around it?

Alrighty then I got it figured. It turns out that yes, lists within lists are entirely fine. The problem did in fact turn out to be caused by my mapping of the database data into my response classes. For example when there was a shipping address with no orders it would still try to send an empty list instead of a null. I simply changed this :

shippingAddress.orders = new List<Orders>();
foreach(order in databaseShippingAddress.orders)
{
     shippingAddress.orders.Add(order);
}

to this:

if(databaseShippingAddress.orders.Count > 0)
{
   shippingAddress.orders = new List<Orders>();
   foreach(order in databaseShippingAddress.orders)
   {
       shippingAddress.orders.Add(order);
   }
}

Now the list will get sent as null instead of empty which seems to serialize just fine. Thanks @whydoidoit for all your help, thumbs for you.

A workaround appears to be provided at the above post.