hell.jumper
hell.jumper

Reputation: 13

JSON.net Serialize object with nested list of int

I'm having an issue which is the following and hopefully you can assist me:

Using JSON.net to serilize/deserilize from/to domain model named ProductModel which has many properties and attributes, public and private both of them and using JsonProperty to serilize/deserilize attributes, and JsonIgnore to avoid duplicates comming from properties (used as wrappers for attributes). Some properties are generic nested ICollection (of type int and other models) and don't make use of JsonIgnore attribute. When the app receives a JSON it deserilize correctly but when it's time to serilize the object model, only those ICollection of int are not serilized properly, just as an empty JSON array.

public class ProductModel : ModelBase
{
    public ProductModel()
    {
        Product_stores = new List<int>();
        Product_related = new List<int>();
        Product_categories = new List<int>();
        Product_discounts = new BindingList<ProductDiscountModel>();
    }

    [JsonProperty("model")]
    private string model;
    [JsonProperty("location")]
    private string location;
    [JsonProperty("quantity")]
    private int quantity;
    [JsonProperty("product_category")]
    public ICollection<int> Product_categories { get; } // Not serilized but deserilized
    [JsonProperty("product_store")]
    public ICollection<int> Product_stores { get; } // Noy serilized but deserilized
    [JsonProperty("product_related")]
    public ICollection<int> Product_related { get; } // Not serilized but deserilized
    [JsonProperty("product_discount")]
    public ICollection<ProductDiscountModel> Product_discounts { get; } // Serilized/Deserilized correctly
    [JsonProperty("product_id")]
    public new int ID
    {
        get => base.ID;
        set => base.ID = value;
    }

    [JsonIgnore]
    public string Model {
        get => model;
        set
        {
            if (model == value)
                return;
            model = value;
            OnPropertyChanged(nameof(model));
        }
    }
    [JsonIgnore]
    public string Location { 
        get => location;
        set
        {
            if (location == value)
                return;
            location = value;
            OnPropertyChanged(nameof(location));
        }
    }
    [JsonIgnore]
    public int Quantity { 
        get => quantity;
        set
        {
            if (quantity == value)
                return;
            quantity = value;
            OnPropertyChanged(nameof(quantity));
        }
    }
 }

Here the serilize process.

public string Update(ProductModel model) => JsonConvert.DeserializeObject<ResponseMsg>(repository.UpdateProduct(JsonConvert.SerializeObject(model, Formatting.Indented))).Info;

As you can see I'm not using JsonSettings.ContractResolver

Suposing there is a ProductModel instance with supplied data; Here a sample of a serilized ProductModel:

{
 "model": "Product 3",
 "location": "",
 "quantity": 7,
 "product_category": [],
 "product_store": [],
 "product_related": [],
 "product_discount": [
   {
    "customer_group_id": 1,
    "quantity": 0,
    "priority": 1,
    "price": 80.0,
    "date_start": "0000-00-00",
    "date_end": "0000-00-00",
    "product_special_id": 438
   },
   {
    "customer_group_id": 1,
    "quantity": 0,
    "priority": 2,
    "price": 90.0,
    "date_start": "0000-00-00",
    "date_end": "0000-00-00",
    "product_special_id": 439
   }
  ],
 "product_id": 30 
}

Upvotes: 1

Views: 2018

Answers (1)

BehiAlav
BehiAlav

Reputation: 178

If you use JsonConvert.SerializeObject() and making sure the integer lists are filled items correctly in runtime, then it will definitely serialize the ICollection lists into JSON array equivalent.Try the code in an Console app as follows:

  public ProductModel()
    {
        Product_stores = new List<int>();
        Product_related = new List<int>();
        Product_categories = new List<int>() { 1, 2 , 3 , 4};
    }

 var jsonStr = JsonConvert.SerializeObject(new ProductModel() , Formatting.Indented);
        Console.WriteLine(jsonStr);
        Console.ReadLine();

Upvotes: 2

Related Questions