0

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?

Kamerton
  • 315
  • 3
  • 9
  • Do you need to strongly type these objects? Maybe he solution is to leave them in a dynamic, dictionary or JsonObject state. – Richard Hubley Mar 20 '19 at 15:55
  • Hm... Not understend about what type are you asking? I need to strongly type an objects as this is strongly type language :). If you are talking about the property names then I have to set them to equals json properties names to default serialization needs but the json names may change depend on what server I do request. – Kamerton Mar 20 '19 at 16:15
  • You can choose to deserialize to a Dictionary object if the JSON allows it. https://www.newtonsoft.com/json/help/html/DeserializeDictionary.htm – Richard Hubley Mar 20 '19 at 16:58
  • 1
    The contract resolver creates a serialization contract for a specific *`Type`*. That contract is typically cached for the duration of the AppDomain. Are your dynamic property names constant for a given type for the duration of the app, or can the change from instance to instance of the same type? Also, can you share a [mcve]? – dbc Mar 20 '19 at 17:31
  • 1
    Thank you for refference about serialization contract. After more deep searching around it i had found an answer on simular qestion: https://stackoverflow.com/questions/38111298/json-net-deserialize-or-serialize-json-string-and-map-properties-to-different-pr I just did missunderstand the `DefaultContractResolver.CreateProperty` behavior. I thought that it works on serialization only but it do in both directions. – Kamerton Mar 21 '19 at 09:15

0 Answers0