iphone - Detect Missing JSON Keys With Nested Objects -


currently in process of working api still in development. due this, keys in response still changing. have been able retrieve , parse json data api nsdictionary, , use nsdictionary map values custom objects. approach using following

-(id)initwithdictionary:(nsdictionary*)dictionary {     if(self = [super init]){         _id = [dictionary valueforkey:kkey_id];         _name = [dictionary valueforkey:kkey_name];         _nestedobject = [[nestedobject alloc]initwithdictionary:[dictionary valueforkey:kkey_nested_object]];         //etc...     }     return self } 

each nested object contains same parsing structure.

this works fine except when api changes. when change, required values not exist , causes unexpected behavior or crashes.

ideally, if 1 of keys change, produce nserror can use print value has changed helping me more find change , rectify it.

the alternative approach have been able come feel messy , unmaintainable.

-(id)initwithdictionary:(nsdictionary*)dictionary anderror:(nserror**)error {     if(self = [super init]){          bool _parsedsuccessfully = true;          if (_parsedsuccessfully) {              _id = [dictionary valueforkey: kkey_id];               if (!_id){                  _parsedsuccessfully = false;                  *error = [nserror parsingerrorfromkey: kkey_id];              }         }          if (_parsedsuccessfully) {              _name = [dictionary valueforkey: kkey_name];               if (!_name){                  _parsedsuccessfully = false;                  *error = [nserror parsingerrorfromkey: kkey_name];              }         }          if (_parsedsuccessfully) {              _nestedobject = [[nestedobject alloc]initwithdictionary:[dictionary valueforkey:kkey_nested_object]];               if (!_nestedobject){                  _parsedsuccessfully = false;                  *error = [nserror parsingerrorfromkey: kkey_nested_object];              }         }          //etc...          if (!_parsedsuccessfully) {             return nil;         }     }     return self } 

i wondering if else had other better approaches preferably uses less duplication.

any appreciated.

add isvalid method object, can used in situation, not when initialised json dictionary.

- (bool)isvalid:(nserror **)error {      #define check_not_null(x, key) if (!x) { \         if (error != null) \             *error = [nserror parsingerrorfromkey:key]; \         return no; \     }      #define check_not_empty(x, key) if (!x || ![x length]) { \         if (error != null) \             *error = [nserror parsingerrorfromkey:key]; \         return no; \     }      check_not_null(_id, kkey_id);     check_not_empty(_name, kkey_name);     // etc.      return yes;     #undef check_not_null     #undef check_not_empty } 

and use in init method:

- (id)initwithdictionary:(nsdictionary*)dictionary anderror:(nserror**)error {     if (self = [super init]) {          _id = [dictionary valueforkey: kkey_id];          _name = [dictionary valueforkey: kkey_name];          // etc.           if (![self isvalid:error]) {              self = nil;    // assuming arc          }     }     return self; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -