So i have a REST API that works with a JSON based on OData exchange. In a OData type there is a ID property i want to read so i can do some checking on it. However when i want to write back to the webserver the ID property must not be present in the response JSON string. So it has to be a write-only property, but simply changing a property to write-only prevents be to check what the value is of that property.
For example, i create a new product:
Public Class Product
Public property ID as integer
Public property Title as string
End class
GET response:
{
"ID" = 1,
"Title" = "Cool product!"
}
POST Wrong:
{
"ID" = 1, <---- ignore this value
"Title" = "Cool product! Changed!"
}
POST Should be:
{
"Title" = "Cool product! Changed!"
}
The webserver uses OData
Using the attribute JsonIgnore
doesn't fix it because the value of the REST response isn't serialized then.
This is for WPF and not ASP.Net
JSON.NET supports conditional property serialization using a method that returns a bool
and has the same name as the property with a ShouldSerialize prefix:
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public bool ShouldSerializeId() => false;
}