Why NSJSONSerialization uses NSData instead of NSString? - objective-c

Is there any reason for NSJSONSerialization to use NSData instead of NSString for representing JSON data?
NSString seems like a more obvious choice to me...

I imagine it would be more efficient to encourage parsing NSData instead of NSString. If you are parsing a response from a server, for example, you'll get an NSData object representing a buffer of raw bytes returned from the server (note that NSJSONSerialization also includes a method for parsing an NSInputStream directly). Parsing the whole thing into an NSString would be a waste since that would just be an intermediate object that would get thrown out. Instead, NSJSONSerialization is probably parsing the bytes in the NSData object directly and only construct NSStrings for the appropriate keys and values in the resulting data structure.

Related

How to NSLog NSData JSON response in JSON format?

Is there a way I can NSLog JSON response of NSData in JSONformat?
NSLog(#"JSON NSString: %#" ,jsonData);
In this post they are printing NSDictionary,I can convert it to NSDictionary. and this solution returns (null).
How can I NSLog in JSON format?
• What's wrong:
jsonData (as you gave) IS NOT a hexData representing a JSON.
• Quick hack (not viable solution!) to get your JSON to use in your site CodeBeautify:
NSDictionary *dictFromData = [NSKeyedUnarchiver unarchiveObjectWithData:jsonData];
NSData *realJSONData = [NSJSONSerialization dataWithJSONObject:dictFromData options:0 error:nil];
NSString *strFINAL = [[NSString alloc] initWithData:realJSONData encoding:NSUTF8StringEncoding];
NSLog(#"StrFINAL: %#", strFINAL);
Note: Yeah, I bypassed the error parameters, and we shouldn't. With
NSJSONWritingPrettyPrinted instead of 0 in options: parameter, you have a result almost similar to the one of CodeBeautify.
• How did I get there:
Firt, I copy/paste your bump string of NSData with this answer.
That way, I got jsonData as you got.
Then, I tried simply what it should be given your informations:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&errorJSON];
Which didn't work giving the error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be
completed. (Cocoa error 3840.)" (Invalid value around character 0.)
UserInfo=0x17598540 {NSDebugDescription=Invalid value around character
0.}
But with NSDictionary *dictWithData = [NSKeyedUnarchiver unarchiveObjectWithData:jsonData];, I managed to get the real NSDictionary. But NSKeyedArchiver/NSKeyedUnarchiver are doing something "equivalent" to NSJSONSerialization: it serializes, transforming a NSObject into NSData (and vice-versa). But more powerful: for any kind of object that are NSCoding compliant. Here, since it's originally from a JSON (only NSString, NSNumber, NSArray and NSDictionary objects, and not a custom one), it's working without any more code.
Did you for instance tried to save it into NSUserDefaults and it's not a .plist either (that was also one on my tries, I saved jsonData into memory, and used dictionaryWithContentsOfFile: giving me weird answer, but a important one in the bump of it: ""$class" = "{value = 23}";" which lead me to NSKeyArchiver/NSKeyUnarchiver). I don't know what you did exactly.
• Conclusion:
So clearly, somewhere, you mixed stuff found on the web. You need to rework that. You can't let it like this. There is issue in your code elsewhere. Where did you get jsonData from? What did you do with it?
Code:
NSLog("Formatted JSON : %#" ,[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
There are different situations. If parsing the JSON data was fine, then you just want to log the result (dictionary or array). If parsing the JSON data failed, and you suspect there is something wrong with the JSON data, then you convert the JSON data to an NSString and log that. And finally, if either conversion to NSString failed, or you look at the NSString and can't find what's wrong with it, then you log the NSData itself to be able to see the bytes. That's useful if someone managed to put control characters or some other nonsense into your JSON data.
The best is to write a method (warning! not for the timid! requires writing code yourself) that takes the NSData, analyses it and prints out the information that you need.

Convert NSValue to NSData and back again, with the correct type

I would like to be able to convert an objective-c object, such as an NSArray or UIImage into an NSData object, which I can then use to write to disk. I first converted them to an NSValue, which I then planned on converting to NSData. This question provided part of the answer, but as they were working with NSNumber, they didn't really have a need to convert it back to NSValue.
I have seen other questions such as this one that relies on NSKeyedArchiver, but I would like to steer away from this due to the vast size inflation that occurs.
Therefore my code at the moment for encoding an NSData object from an NSValue, from the first question, is as follows:
+(NSData*) dataWithValue:(NSValue*)value {
NSUInteger size;
const char* encoding = [value objCType];
NSGetSizeAndAlignment(encoding, &size, NULL);
void* ptr = malloc(size);
[value getValue:ptr];
NSData* data = [NSData dataWithBytes:ptr length:size];
free(ptr);
return data;
}
My question is how would I go about decoding an NSData object that has been encoded in this manner and get back the original objCType from the NSValue?
I would assume I would be using something along these lines
[NSValue valueWithBytes:[data bytes] objCType:typeHere];
How would I get back the type information?
Use NSKeyedArchiver to save the items and NSKeyedUnarchiver to restore the object. The object must conform to NSCoding and in the case of a collection all contained items must also conform to NSCoding.
See the Apple documentation of NSKeyedArchiver and NSCoder
Your approach will only work for primitive types (int, float, structs without pointers, ...) inside your NSValue. Otherwise you will only get the meaningless pointer value but not the actual data in your NSData object.
To also pass the actual type string along you would have to figure out a way to get this inside your NSData object as well. Not impossible, but it will not solve your actual problem.
Using a keyed archiver as zaph suggests is much better.

NSString substituting NSData possible, what are consequences?

Suppose I'm storing a stream of ASCII, say 0x0a0b0c00. What would happen to the data if I store it in an NSData instance vs. an NSString? Would the data get converted into something else? I'm a little confused because they are both buffers holding the exact same thing.
NSData is a container to store, as its name suggests, raw binary data. NSData makes no assumptions of the format of the binary data. It can be text, images, audio, etc.
NSString interprets the data as text with a given encoding: which could be ASCII, Unicode, etc. In most cases, NSString will copy the bytes to its internal data structure to store the raw binary.
If it's not text, use NSData. It's clearer in code to know what's being managed and avoids having to fight string encodings.

Building a custom NSArchiver serialize to string

How does NSArchiver serialize to file? I assume it's serialized in binary format, is that correct? What if I want to store it in string so I can store into SQLite database? Do I need to write my own custom NSArchiver? If so, how do I go about doing that? Are there any tutorials out there?
p.s. I do realize Core Data can do this but let me cross that option out for now.
You can archive to an NSData object instead of to a file, if you want, with +archivedDataWithRootObject:. It won't be a "string," but that's fine, because an NSString in Cocoa represents a sequence of Unicode characters, while an NSData represents a sequence of bytes (which you could easily store wherever you want, including in a database).
Note that you really should be using NSKeyedArchiver instead:
+ (NSData *)archivedDataWithRootObject:(id)rootObject
+ (id)unarchiveObjectWithData:(NSData *)data

NSData or NSAttributedString with SBJSON

I am using the SBJSON to convert my NSDictionary to a JSON String in my iOS application.
when my dictionary contains a NSAttributedString or an NSData, the SBJSON fails to generate the string representaiton.
Incase of NSAttributedString, the error is :
-JSONRepresentation failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON serialisation not supported for NSConcreteMutableAttributedString\
Incase of NSData, the error is :
-JSONRepresentation failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON serialisation not supported for NSConcreteMutableData\"
UserInfo=0x7ed2560 {NSLocalizedDescription=JSON serialisation not
supported for NSConcreteMutableData}"
Solving atleast one of the 2 problems will be a great deal.
Please help.
Thanks
Roshit
JSON doesn't have any datatype to do what you want, but you could convert the NSData into a Base64 encoded string. This can be done automatically with a category on NSData that implements the -proxyForJson method. The problem is when you need to convert it back to NSData on the other end. If the key is known, then you can just Base64 decode that key. But if the data portion can be for any key it's a bit more difficult. You'll have to somehow structure your data so you can determine which strings should be Base64 decoded.
You can not pass NSData object. Solution for problem is, Just use the following line (change response to your nsdata object) and use that string as a value.
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
You can not pass NSAttributedString as a value as well. You have to change NSAttributedString to NSString. Please check OHAttributedLabel lib for more information.