Object-oriented programming projects almost always start by the design of the business objects model. iPhone and iPad projects aren’t exempt from this rule, but wouldn’t it be nice to significantly speed up the creation of your business objects classes, the parsing code and to lessen the burden of keeping up with simple changes? Enter BkJsonSchema, a complete suite of code generation tools based on JSON Schema (which in turn relies on the industry standard JSON data exchange format).
BkJsonSchema is a Google Code project containing two (2) tools that can generate parsing code for JSON object files (for now it generates Objective C code, but in the future it will generate Java parsing code based on Jackson). Both tools are based around simple extensions to the JSON Schema standard to better describe data types useful in complex parsing code. For instance, we added the mappedType key to specify the mapped class in the destination language.
- The BkCoreData2JsonSchema Xcode project builds a command line tool that can generate a JSON schema file describing the entities (attributes and relationships) contained in a Core Data managed object model (.xcdatamodel files).
- jsonschema2objc is Ruby script that can generate Objective C header and implementation files (.h/.m) containing efficient parsing code taking as input an NSDictionary. The NSDictionary is most often created by converting the raw JSON object file using JSON framework parser.
- A TextMate bundle called BkJsonSchema which adds a handful of keyboard shortcuts that can really speed up the process of editing the JSON mapping schemas.
These tools can be used to generate code compatible with Core Data, as well as code that does not rely on Core Data in any way (simple business objects that are NSObject subclasses). The generated NSObject subclasses are NSCoding and NSCopying compliant as well.
It is not uncommon to mix both modes provided by these tools (Core Data and non-Core Data parsers) in the same application. It is recommended to generate the output in specific directories (one for persistent entities, one for transient objects). To achieve this, you start by designing your Core Data managed object model using the Xcode graphical editor. Make sure to specify the output class name for each entity (by adding a project prefix). You can optionally specify alternate mappings for each attribute. That is especially useful if, for instance, the JSON file provided as input to the parser contains a myFirstName key and you’d like the generated business object to contain a property named firstName instead. The mapping can be specified by adding a remote key to the userInfo of each attribute (in the example you would add an attribute called firstName and add a userInfo key called remote with the value myFirstName). Once you’re done tweaking your Core Data model, run the coredata2jsonschema tool with the –coredata command line option.
Although, coredata2jsonschema spits out the JSON schema on STDOUT, so you can conveniently pipe it into the jsonschema2objc script, it is recommended that you write the output as a separate file. From there, you can generate the parsing code to a specific directory (note that jsonschema2objc will create an original-output directory that is necessary for three-way merging of revisions of your schema).
In the case you want to generate NSObject subclasses, we recommend you install the TextMate bundle and define your schema manually. Run the jsonschema2objc against this other JSON schema file. Again, we recommend redirecting the output to a specific directory you can manage NSObject subclasses separately from the NSManagedObject subclasses (for instance ./transient-model/ and ./persistent-model/).
The project is available under the GNU Public License v3 on Google Code.
http://code.google.com/p/bkjsonschema
Enjoy !
François Proulx




