Reputation: 766
I have a NSMutableArray with custom objects. The objects inside have a NSData field. I want to transport them to my web service using JSON format but I don't really know how to do it with NSJSONSerialization as it doesn't support NSData. Can you tell me if it's possible and provide me with some sample code or some other library that can handle this?
Upvotes: 0
Views: 499
Reputation:
No. As the NSJSONSerialization
documentation makes clear:
An object that may be converted to JSON must have the following properties:
- The top level object is an
NSArray
orNSDictionary
.- All objects are instances of
NSString
,NSNumber
,NSArray
,NSDictionary
, orNSNull
.- All dictionary keys are instances of
NSString
.- Numbers are not NaN or infinity.
NSData
obviously doesn't have those properties.
So likely what you'll want to do is encode it as a Base64 string or some other form you consider acceptable (there are plenty of options other than Base64, but it's the one that immediately comes to mind). You might also be able to get by with just converting it to a string (depending on what the data is) that uses the appropriate escape codes and so on, though you should make sure that won't result in any encoding issues.
Anyway, the answer's no. Find a way to encode it as an NSString
or something else NSJSONSerialization
can work with.
Upvotes: 7