Reputation: 3381
I have a simple class:
public class ProductListStoredModel: List<Product>
{
public int NextId { get; set; }
}
If I use:
ProductListStoredModel stored_model = ...
string new_json = JsonConvert.SerializeObject(stored_model);
It will happily serialise the list. However, it doesn't add 'NextId' to the JSON. Is there a simple way to do that (like adding an attribute)?
Upvotes: 2
Views: 6951
Reputation: 1587
class Program
{
static void Main()
{
ProductListStoredModel stored_model = new ProductListStoredModel();
Product p = new Product();
stored_model.Add(p);
stored_model.NextId = 10;
stored_model.Pro = stored_model[0];
string new_json = JsonConvert.SerializeObject(stored_model);
Console.WriteLine(new_json);
Console.ReadLine();
}
}
[JsonObject(MemberSerialization.OptIn)]
public class ProductListStoredModel : List<Product>
{
//public Product[] pro { get; set; }
[JsonProperty]
public int NextId { get; set; }
[JsonProperty]
public Product Pro { get; set; }
}
you can make an array property of product as well and can continue so on.
public Product[] Pro { get; set; }
Upvotes: 2