I am pulling data from an API that allows for custom fields which results in the JSON response changing structure for different API calls. I am using RestSharp/SimpleJSON to deserialize the JSON into an object defined in my project. My users are able to configure their accounts to pull their own custom fields, but my issue is that I'm not able to deserialize new custom fields into my object without adding that property to the class, recompiling, and deploying the changes. Does anyone have an idea how I could access this new custom field without having to add the property to the class and redeploying? Examples below...
definition
public class UserFields
{
public string Username {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string GradeCustomFieldName {get;set;}
public string CustomfieldABC {get;set;} //user ABC's custom field for "grade"
public string CustomfieldXYZ {get;set;} //user XYZ's custom field for "grade"
public string CustomfieldLMNO {get;set;} //user LMNO's custom field for "grade"
public string CustomfieldASDF {get;set;} //user ASDF's custom field for "grade"
public string UserGrade
{
string grade = this.GetType().GetProperty(this.gradeCustomFieldName).GetValue(this);
}
...
}
usage
UserFields userFields = SimpleJson.DeserializeObject<UserFields>(jsonResponse);
string userGrade = userFields.UserGrade;
json response for client ABC
{
"Username": "bobslydale",
"FirstName": "bob",
"LastName": "slydale",
"GradeCustomFieldName": "CustomfieldABC",
"CustomfieldABC": "A"
}
json response for client XYZ
{
"Username": "bobslydale",
"FirstName": "bob",
"LastName": "slydale",
"GradeCustomFieldName": "CustomfieldXYZ",
"CustomfieldXYZ": "C"
}
My issue is that when client OIU is added and their GradeCustomFieldName is CustomfieldOIU, I have to add it to my class definition before they can use the app.
JSONor store it as key value pairs, then serialize it into json and send it to the client.