Given the following simple class:
public class Person
{
  public int id { get; set; }
  public List<Order> orders { get; set; }
}
When the orders property is null, and once the Person instance is serialized using JSON.NET below:
var json = JsonConvert.SerializeObject(myPerson);
I need the physical output returned (it's an existing constraint) to look like the following when orders = null:
{
 "id": 1,
 "orders": []
}
However, what is happening is that the orders property is being returned with all null values like below:
{
 "id": 1,
 "orders": [
    {
      "id": null,
      "total":null,
      "orderDate":null,
      "itemQuantity":null
    }
  ]
}
I can't have that collection be returned if null but rather it needs to show as basically an empty array like: "orders": []"
I've tried:
myPerson.orders = null;
However, this still produces the full object collection being displayed with all null values.
How can I intervene on the serialization or manipulation of this object so I can have it physically return as an empty array as opposed to an expanded null collection of fields?
EDIT: A portion of the question was answered via the comments. The full blown object instance being returned was due to a LINQ query returning a resultset that instantiated a default instance OF orders. However the main part of the question is still about making it return "orders":[] and not "orders":null
Orderand adding it to the list -- possibly in some converter or default constructor. What does the default constructor forPersonlook like?nullwould be returned for the property as a whole and I still need[]JsonConvert.SerializeObject(new Person() { id = 1, orders = null })gives me{"id":1,"orders":null}."orders":nullHowever, I still need to make it be"orders":[]Should I go brute force and find/replace after serialization or is there a serializer setting/attribute that can do this?person.orders = new List<Order>()then it should be an empty array instead ofnull.