Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson equivalent for iPhone?

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.

like image 408
Usman Ismail Avatar asked Nov 21 '11 01:11

Usman Ismail


2 Answers

You might try GoldenFleece, which converts between JSON and Objective-C objects using a convention-over-configuration pattern inspired by Jackson.

like image 99
Alex Nauda Avatar answered Oct 18 '22 06:10

Alex Nauda


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
like image 2
Wayne Hartman Avatar answered Oct 18 '22 08:10

Wayne Hartman