• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSCoding to/from JSON?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: NSCoding to/from JSON?
      • From: Sean McBride <email@hidden>
    • Re: NSCoding to/from JSON?
      • From: "email@hidden" <email@hidden>
    • Re: NSCoding to/from JSON?
      • From: Mike Abdullah <email@hidden>
    • Re: NSCoding to/from JSON?
      • From: Andy Lee <email@hidden>
  • Prev by Date: Re: Drawing text like Lion's Mail
  • Next by Date: Re: Asynchronous downloading and parsing of XML
  • Previous by thread: Re: Release build throw "unrecognized selector sent to instance" for a category method
  • Next by thread: Re: NSCoding to/from JSON?
  • Index(es):
    • Date
    • Thread