I have a RESTful webservice which receives JSON and it deserialises it into a c# class using the DataContractJsonSerializer, though this can be changed.
Its purpose is to update fields on a resource
e.g.:
{
"firstName" : "Martin"
}
I don't necessarily want to update all the fields, and I was hoping to find a way to detect fields which are and are not unspecified in the JSON.
I can't find a way to do this however because I don't know how to tell the difference between an unspecified field and a field which should be updated to null
e.g: (don't update any fields):
{}
vs: (update the firstName field to null)
{
"firstName" : null
}
What is the best way to approach this?
I think you can use a field initialized with some random string
If you deserialize using
{"firstName" : null}
, firstName will be null. if you deserialize using{}
firstName won't change (some string
)PS: Don't use
DataContract
orDataMember
attributes if you are using DataContractJsonSerializer