I want to make my json deserializer ignore objects with improper values (like string in place of a int) or return null for them and continue to deserialize the rest of a json file.
Here is my json:
{
"requests":[
{
"clientId":"1",
"requestId":"1",
"name":"Bułka",
"quantity":"1",
"price":"10.00"
},
{
"clientId":"1",
"requestId":"2.1",
"name":"Chleb",
"quantity":"2",
"price":"15.00"
},
{
"clientId":"1",
"requestId":"2",
"name":"Chleb",
"quantity":"5",
"price":"15.00"
},
{
"clientId":"2",
"requestId":"1",
"name":"Chleb",
"quantity":"1",
"price":"10.00"
}
]
}
Here are the classes I'm deserializing to:
class RequestCollection
{
public List<Request> requests { get; set; }
public RequestCollection()
{
requests = new List<Request>();
}
}
class Request
{
public string clientId { get; set; }
public long requestId { get; set; }
public string name { get; set; }
public int quantity { get; set; }
public double price { get; set; }
public Request() { }
public Request(string clientID, long requestID, string name, int quantity, double price)
{
this.clientId = clientID;
this.requestId = requestID;
this.name = name;
this.quantity = quantity;
this.price = price;
}
}
And here is how I deserialize the file:
requestCollectionLocal = JsonConvert.DeserializeObject<RequestCollection>(json);
As you can see I have improper value of requestId in 2nd object in json file. I want the result of deserialization to be just 3 other objects or all 4 objects with null values instead of improper ones.