I have used Jackson extensively on the Server side to convert from POJOs to JSON and was wondering if there is a similar library for Objective C/iPhone SDK and vice versa. Objective C does provide reflection so it should be possible to make something similar to Jackson.
-
Just to be clear since there may be a language barrier here; you want code to serialise objects into JSON automatically, based on their declared properties? – Tommy Nov 21 '11 at 01:35
-
Yes, The other way around would be useful too because our server sends JSON encoded response. – Usman Ismail Nov 21 '11 at 23:33
3 Answers
You might try GoldenFleece, which converts between JSON and Objective-C objects using a convention-over-configuration pattern inspired by Jackson.

- 4,126
- 1
- 24
- 24
-
1This is about a year too late for me and I notice the self-promotion but I appreciate the your effort writing this library. The current state of the art was painful. – Usman Ismail Feb 19 '14 at 15:43
The new iOS 5 APIs provide a great facility in reading/writing JSON. These are essentially a rehash of the TouchJSON library which you can use in iOS 4. While I haven't seen much out there that will generate POCO objects from an example payload, you can create classes that are just a facade for an NSDictionary
instance that the aforementioned libraries will return.
For example:
@interface PBPhoto : NSObject {
NSDictionary* data_;
}
@property (nonatomic, retain, readonly) NSDictionary *data;
- (NSString*) photoId;
- (NSString*) userId;
- (NSString*) user;
- (NSString*) title;
- (id) initWithData:(NSDictionary*)data;
@end
Implementation:
#import "PBPhoto.h"
#define PHOTO_ID @"id"
#define USER_ID @"user_id"
#define USER @"user"
#define TITLE @"title"
@implementation PBPhoto
@synthesize data = data_;
- (id) initWithData:(NSDictionary*)data {
if ((self = [super init])) {
self.data = data;
}
return self;
}
- (NSString*) photoId {
return [super.data objectForKey:PHOTO_ID];
}
- (NSString*) userId {
return [self.data objectForKey:USER_ID];
}
- (NSString*) user {
return [self.data objectForKey:USER];
}
- (NSString*) title {
return [self.data objectForKey:TITLE];
}
- (void) dealloc {
[data_ release];
[super dealloc];
}
@end

- 18,369
- 7
- 84
- 116
-
1It would be useful not to have to implement the getters for every property of every class. That's where reflection may be useful. – Usman Ismail Nov 21 '11 at 23:35
That Objective-C provides reflection may be the understatement of the year, but a lot of stuff is exposed only by the low-level C runtime and therefore is a little obtuse.
Assuming you want to take an arbitrary object and turn it into JSON, probably the smart thing is to create an NSDictionary
as an intermediary, then pass it off to NSJSONSerialization
(or else construct the string yourself because all of the third party libraries are quite heavyweight owing to the burden of being able to deserialise).
So, for example:
- (NSDictionary *)dictionaryOfPropertiesForObject:(id)object
{
// somewhere to store the results
NSMutableDictionary *result = [NSMutableDictionary dictionary];
// we'll grab properties for this class and every superclass
// other than NSObject
Class classOfObject = [object class];
while(![classOfObject isEqual:[NSObject class]])
{
// ask the runtime to give us a C array of the properties defined
// for this class (which doesn't include those for the superclass)
unsigned int numberOfProperties;
objc_property_t *properties =
class_copyPropertyList(classOfObject, &numberOfProperties);
// go through each property in turn...
for(
int propertyNumber = 0;
propertyNumber < numberOfProperties;
propertyNumber++)
{
// get the name and convert it to an NSString
NSString *nameOfProperty = [NSString stringWithUTF8String:
property_getName(properties[propertyNumber])];
// use key-value coding to get the property value
id propertyValue = [object valueForKey:nameOfProperty];
// add the value to the dictionary —
// we'll want to transmit NULLs, even though an NSDictionary
// can't store nils
[result
setObject:propertyValue ? propertyValue : [NSNull null]
forKey:nameOfProperty];
}
// we took a copy of the property list, so...
free(properties);
// we'll want to consider the superclass too
classOfObject = [classOfObject superclass];
}
// return the dictionary
return result;
}
Then you can use + dataWithJSONObject:options:error:
on NSJSONSerialization
with the returned dictionary.
To go the other way, I guess you'd use the key-value coding setValue:forKey:
method, getting keys and values from a dictionary via allKeys
and valueForKey:
.

- 99,986
- 12
- 185
- 204
-
Thanks, yeah this is what I had in mind too but hoped that someone had done the hard work for me. I think I will use your example as a basis and whip something up myself – Usman Ismail Nov 21 '11 at 23:38