I donβt know of any specific implementations, but the key value of coding is very close to you: Guide to coding key values . I had good results combining streaming json parsing with KVC.
The -setValue: forKey: method makes it much easier to adapt serialized data to user objects. To continue your example, you must create a Unicorn class with all the necessary access methods: -setName: / - name, -setManeColor / -maneColor, etc. (You may be able to use properties for some expected values, but for example, with the value maneColor, where you probably want to write a custom setter to convert from a color name string to an NSColor or UIColor object.)
You will also want to add two more methods to your custom object: -setValue: forUndefinedKey: and -valueForUndefinedKey :. These are methods that will be called if your object does not have access methods that correspond to the key passed to the KVC methods. Here you can get unexpected or unsupported values ββand store them or ignore them as needed.
When you send -setValue: forKey: to a Unicorn object, the structure looks for accessors matching the key pattern. For example, if the key is "maneColor" and you set the value, the environment checks if your object implements -setManeColor :. If so, it calls this method, passing the value; otherwise, -setValue: forUndefinedKey: is called, and if your object does not implement it, an exception is thrown.
When your parser delegate is notified of the start of parsing the json unicorn object, create an instance of the Unicorn object. When your parser returns the parsed data to you, use -setValue: forKey: to add 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 ]; }
more tension
source share