Over at the Big Nerd Ranch blog, my friend Mark Dalrymple continues his "Inside the Bracket" series with an article on practical uses of Objective-C's run time introspection.
Last time you saw the parts of the Objective-C runtime API that let you query a bunch of the metadata the compiler keeps around once it’s done building your project. Like most discussions of API, it was pretty abstract. “Look at all the pretty tools! Ooh, we can print out Lists of Stuff! Isn’t that amazing!”
This time around is an actual application of this API for Great Justice. I was working on a client project that consumed blobs of JSON data from a third-party web service.
Converting JSON to Objective-C objects is fraught with some peril, but with the code Mark presents it becomes a lot safer.
Mark's code, incidentally, is an improved version of some code I wrote and discussed on Cocoa is My Girlfriend. Mark's version is better, though.
Mac OS X and iOS include support for JSON and property lists, two generic structured data formats, both in common use in different places. If Stack Overflow is any indication, there's a fair amount of confusion regarding the two. It's true, they're very similar, so much that it's tempting to think the differences are merely syntax choices. In some cases it's possible to read data from one format and write it to the other as-is, with no conversion. But not always. JSON isn't a property list and a property list isn't JSON, but they do have a lot in common. Both of these are generic data formats, in that they're designed to be flexible enough to store structured data for a wide variety of uses. For any given use, neither is likely to be the…