I'm working on a project where I need to deserialize a JSON api response, and do things with the data. This is a little demo thing:
class Response { [JsonProperty("success")] [DefaultValue(false)] public bool Success { get; private set; } [JsonProperty("days")] public Days Days { get; private set; }}class Days { [JsonProperty("first_day")] [DefaultValue("monday")] public string FirstDay { get; private set; }}
class Response { [JsonProperty("success")] [DefaultValue(false)] public bool Success { get; private set; } [JsonProperty("days")] public Days Days { get; private set; }}class Days { [JsonProperty("first_day")] [DefaultValue("monday")] public string FirstDay { get; private set; }}
My issue is, I need to be able to use
Response.Days.FirstDay
Response.Days.FirstDay
, when my JSON response looks like this:
{'success': true, 'first_night': 'friday'}
{'success': true, 'first_night': 'friday'}
Essentially, I want the
DefaultValue
DefaultValue
to be present, even if the JSON doesn't contain the
'first_day'
'first_day'
key. I'm really not sure how to do this, because I'd need to do this with dozens of attributes in about 6 subclasses, so doing it all manually would take ages and doesn't seem good for maintainability.
Any help on how I can achieve this result? To get everything to 'exist' even if it doesn't exist in the JSON, just by using the default value?