When deserializing a Json object in a .Net type , if the field names do not match, I find that you can decorate your type properties [JsonProperty(PropertyName = "name")]
This is fine and dandy for several properties that do not match, but is there a way to set an agreement or a rule?
Json
{ "Job": [ { "Job #": "1", "Job Type": "A", } ] }
WITH#
[JsonProperty(PropertyName = "Job Type")] public string JobType { get; set; } [JsonProperty(PropertyName = "Job #")] public string JobNumber { get; set; }
I have many fields with similar names that I would like to find out if there is a way to say to set a rule to always remove spaces (EG: Job Type -> JobType ) and replace # with Number (for example: Job # -> JobNumber )?
It seems like a custom ContractResolver might be the only solution, but I can't figure out how to use it to tear out spaces and replace "#" with "Number". Does anyone have a reference example?
Or, I hope, there will be a nice simple solution that I missed.
PS We also accept offers for a better name.
source share