NSCoding to/from JSON?
NSCoding to/from JSON?
- Subject: NSCoding to/from JSON?
- From: Jens Alfke <email@hidden>
- Date: Mon, 08 Aug 2011 09:56:07 -0700
Been thinking about archiving NSObjects to/from JSON, using an API similar to NSCoding. I haven’t found any prior art, but I thought I’d ask here.
I am not talking about serializing JSON to/from collection objects a la TouchJSON, JSONKit, etc.; rather, something that lets you convert your custom model objects into JSON and back. And I want it to be clean JSON, so that you can use this API to unarchive JSON created by any of the zillions of web APIs that generate it.
It’s tempting to try to do this using the existing NSArchiving protocol and writing a custom NSKeyed[Un]Archiver subclass. This doesn’t work, at least for my purposes, because JSON has a limited set of data types and I don’t want to clutter up the output with extra type annotations. For example, say the JSON contains a date. JSON doesn’t have a date type, so the convention is to represent the date as a string in ISO-8661 format. But calling -decodeObjectForKey: will return an NSString instead of an NSDate. To get around this you have to add methods like -decodeDateForKey: that make it more clear what you’re expecting. (The same goes for other common value classes like NSData.)
—Jens_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden