ios - Is there a way to normalize json data from NSJSONSerialization? -
it seems since xcode 6.1, iphone 5s, iphone 6 , iphone 6+ simulators (all 64-bit) homecoming info next scheme method differently (keys ordered differently) 32-bit simulator counterparts (e.g. iphone 5 simulator)
+ (nsdata *)datawithjsonobject:(id)obj options:(nsjsonwritingoptions)opt error:(nserror **)error;
this difference of key ordering caused problem me since calculate sha1 of json info (converted nsstring*) , utilize validation test. since ordering changed, sha1 changed , validation fails.
simplified sample code (non-arc) sha1 below:
nsdata* jsondata = [nsjsonserialization datawithjsonobject:dict options:0 error:&error]; nsstring * json = [[[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding] autorelease]; nsstring * sha1 = [myutils computesha1:json]; +(nsstring*) computesha1:(nsstring*)input { const char *cstr = [input cstringusingencoding:nsutf8stringencoding]; nsdata *data = [nsdata datawithbytes:cstr length:input.length]; nsnumber* datalen = [nsnumber numberwithunsignedinteger:data.length]; uint8_t digest[cc_sha1_digest_length]; cc_sha1(data.bytes, datalen.unsignedintvalue, digest); nsmutablestring* output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length * 2]; for(int = 0; < cc_sha1_digest_length; i++) [output appendformat:@"%02x", digest[i]]; homecoming output; }
apparently, key ordering difference doesn't happen on actual devices (previous behavior preserved).
i tried nsjsonwritingprettyprinted
alternative json ordering still inconsistent between simulators.
so, question is: have recommendation on how normalize such json info not susceptible key ordering changes? alternately, there way previous (32-bit simulator) behavior?
key ordering in dictionaries not guaranteed. if need them sorted, set them array , sort them.
ios objective-c xcode
No comments:
Post a Comment