I need help to customize deserialization in runtime with json.net.
In my case i know an accepting json model property names in run time only so I can't use the standart JsonPropertyAttribute
to set the name.
The method DefaultContractResolver.ResolvePropertyName looks good for overriding for me but it have no information about a target type of a model. And that is the main problem for me now.
In my solution I have the custom attributes with a key names. I set them on the "dynamic" properties to map them to known values in runtime.
Model Example:
public class MyDeserializationModel
{
public string known_property { get; set; }
[MyAttribute(Constants.CustomFields.DynamicProperty1)]
public string DynamicProperty1 { get; set; }
[MyAttribute(Constants.CustomFields.DynamicProperty2)]
public string DynamicProperty2 { get; set; }
}
So it woud be ideal for me to get information about a target deserialization type to get the attribute value to generate the name to map.
I have founded some soultions with creating a dictionary with KeyValuePair:TypePropertyName|MyPropertyName
on initialize inside my CustomFieldContractResolver
but it looks annoying to customize SerializationSettings
on every request to set ContractResolver
for awaiting response types. And after all I must to generate unique property names for a several models that may be inside a singe json response.
Can I override property mapping on deserialization with getting an information about the target type and the json property name somehow?