0

I am working on a project where I am using Refit library for making Http calls and parsing the data to specific models. This works well for most of it, however, I have an issue where I have parse the Json to pre-defined models at run time. As an example the response below from the API can vary based on what actions the user of the app has to take. My question is how to parse the "actions" field from the Json to a specific model. I have pre-defined all the models for which the values can be mapped. Like I have a "ResetPassword", "TwoFactorAuthnentication" etc etc. I would like to use a TypeAdapter or something equivalent to be able to parse the value to specific model.

Any help will be greatly appreciated.

   "authorizeActions": [
        {
            "action": "",
            "required": true,
            "webFallback": true
         }
    ]

   "authorizeActions": [
        {
             "action": "resetPassword",
             "required": true,
             "webFallback": true
         }
     ]

Below is the model which I am using currently

public class AuthorizeActions
{

    [JsonProperty(PropertyName = "required")]
    public bool Required { get; set; }


    [JsonProperty(PropertyName = "webFallback")]
    public bool WebFallback { get; set; }


    //Here i need to define the action , what I want is to be parsed to a 
    concrete class like, however the value can change, one time it can
    be "ResetPassword" but it can also be "TwoFactorAuthentication" etc etc.

}
  • So, in simpler words: `action` should be mapped against an enumeration of possible actions, not a simple string. – Marco Apr 12 '19 at 10:06
  • Hello Marco, yes this is what I am trying to achieve , I know in java we can use RuntimeTypeAdapterFactory , link : https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java – Todor Kosev Apr 12 '19 at 10:08
  • And what will happen, if action is not in that enumeration? – Marco Apr 12 '19 at 10:10
  • @Marco I have created a class for all possible enumeration, so at runtime it wont happen that there is no mapping. – Todor Kosev Apr 12 '19 at 10:18
  • You can remove `[JsonProperty(PropertyName = "")]`, Json.Net is case incensitive. – aloisdg Apr 12 '19 at 12:03

0 Answers0