Cocoa: deserialize json string to custom objects (not NSDictionary, NSArray) Cocoa: deserialize json string to custom objects (not NSDictionary, NSArray) json json

Cocoa: deserialize json string to custom objects (not NSDictionary, NSArray)


I'm not aware of any specific implementations, but key-value coding gets you very close to what you want: Key Value Coding Guide. I've had good results combining streamed json parsing with KVC.

The -setValue:forKey: method makes adapting serialized data to custom objects fairly straightforward. To continue with your example, you'd create a Unicorn class with all required accessor methods: -setName:/-name, -setManeColor/-maneColor, etc. (You may be able to use properties for some expected values, but there are cases, as with the maneColor value, where you probably want to write a custom setter to convert from the color name string to an NSColor or UIColor object.)

You'll also want to add two more methods to your custom object: -setValue:forUndefinedKey: and -valueForUndefinedKey:. These are the methods that will be called if your object has no accessor methods matching a key passed into the KVC methods. You can catch unexpected or unsupported values here, and store them or ignore them as necessary.

When you send -setValue:forKey: to the Unicorn object, the framework looks for accessors matching the key pattern. For instance, if the key is "maneColor" and you're setting the value, the framework checks to see if your object implements -setManeColor:. If so, it invokes that method, passing in the value; otherwise, -setValue:forUndefinedKey: is called, and if your object doesn't implement it, an exception is thrown.

When your parser's delegate receives notification that parsing a json unicorn object has begun, instantiate a Unicorn object. As your parser returns the parsed data to you, use -setValue:forKey: to add the data to your object:

- ( void )parserDidBeginParsingDictionary: (SomeParser *)p{     self.currentUnicorn = [ Unicorn unicorn ];}- ( void )parser: (SomeParser *)p didParseString: (NSString *)string          forKey: (NSString *)key{    [ self.currentUnicorn setValue: string forKey: key ]}- ( void )parserDidFinishParsingDictionary: (SomeParser *)p{    [ self.unicorns addObject: self.currentUnicorn ];}


Use Jastor - https://github.com/elado/jastorTakes already parsed JSON into NSDictionary and fills an instance of real Objective-C class.

NSDictionary *parsedJSON = (yajl, JSONKit etc)Unicorn *unicorn = [[Unicorn alloc] initWithDictionary:parsedJSON];unicorn.maneColor // "silver"


As any subclass of NSObject conforms to NSKeyValueCoding protocol:

NSDictionary *parsedJSON = //whateverid <NSKeyValueCoding> entity = [[CustomNSObjectSubclass alloc] init];[entity setValuesForKeysWithDictionary:parsedJSON];