When deserializing a Json object into a .Net type, if the field names don't match up I found you can decorate your type's properties with [JsonProperty(PropertyName = "name")]
This is fine and dandy for a couple of properties that don't match up, but is there a way to set a convention or rule?
Json
{
"Job": [
{
"Job #": "1",
"Job Type": "A",
}
]
}
C#
[JsonProperty(PropertyName = "Job Type")]
public string JobType { get; set; }
[JsonProperty(PropertyName = "Job #")]
public string JobNumber { get; set; }
I have many fields using similar names, what I would like to figure out, is there a way to tell to set a rule to always remove spaces (EG: Job Type -> JobType) and replace # with Number (eg: Job # -> JobNumber)?
It looks like a custom ContractResolver might be the only solution, but I can't seem to figure out how to go about using it to pluck out spaces and replace "#" with "Number". Does anyone have a reference example?
Or, I'm hoping there's a nice simple solution that I've overlooked.
P.S. Also accepting suggestions for a better title.