I have an invoice with two lines. On both lines I have the same product item selected. Now I want to deserialize this with Json.NET (former Newtonsoft.Json):
{
"invoiceNumber":"SALES-001",
"lines":[
{
"itemId":1,
"lineDescription":"product with discount",
"quantity":1,
"price":1000,
"item":{
"id":1,
"description":"product",
"salesPrice":1200
}
},
{
"itemId":1,
"lineDescription":"product",
"quantity":1,
"price":1200,
"item":{
"id":1,
"description":"product",
"salesPrice":1200
}
}
]
}
When deserializing this to C# objects I believe for each item a new instance is created, somewhat like:
new Item { Id = 1, Description = "product", SalesPrice = 1200 }
But what I actually want is that in the deserialization process the object instance for item, created on the first line, will be reused on the second line (by means of object comparison?).
Reading the docs I haven't found it. And going through the code I saw JsonSerializerSettings
offers ObjectCreationHandling.Reuse
and PreserveReferencesHandling.Objects
. Only this does not seem to have the desired effect. So I've come to think that this is not possible by configuration.
Any suggestions would be great!