Object Mapping library from JSON to NSObjects Object Mapping library from JSON to NSObjects json json

Object Mapping library from JSON to NSObjects


Consider using RestKit: http://restkit.org

This framework has all you need — REST and JSON abstractions, object mapping, even Core Data support and a lot of really useful stuff, all implemented in a customizable and elegant manner.

UPDATE: Ok, while writing yet another mapping method, I decided I can't do it anymore and done a small framework. It introspects object's properties, and with a little tuning gives you free pretty description, isEqual/hashCode, free NSCoding support, and allows generating to/from JSON mappers (ok, actually, NSDictionary, but who would use it for something else). All NSNull-checks, missing fields in JSON, new unexpected fields in JSON are handled gracefully and reported properly.

If anybody wants this shared to the public, you could give me some upvotes or comments. I'd do that eventually, but I might consider sharing it faster.


Why not add mappings for classes?

+ (NSDictionary *)mapClasses {    return @{            @"category": [Category class],            // ...    };}

For non-container properties, you could even do runtime introspection of properties to avoid redundant mappings.

Container properties could map to special wrapper objects:

[OMContainer arrayWithClass:Key.class], @"keys",[OMContainer dicionaryWithKeyClass:ScopeID.class valueClass:Scope.class], @"possibleScopes",

Or even blocks for dynamic selection of types:

[OMDynamicType typeWithBlock:^(id obj){    if ([obj isKindOfClass:NSString.class] && [obj hasPrefix:@"foo"])        return Foo.class;    else        return Bar.class;}], @"foo", 

Implementing this could go something like:

+ (NSArray *)parseData:(NSData*)jsonData intoObjectsOfType:(Class)objectClass {    NSArray *parsed = /* ... */    NSMutableArray *decoded = [NSMutableArray array];    for (id obj in parsed) {        [decoded addObject:[self decodeRawObject:parsed intoObjectOfType:objectClass]];    }    return decoded;}+ (id)decodeRawObject:(NSDictionary *)dict intoObjectOfType:(Class)objectClass {    // ...    NSDictionary *jsonKeys = objectClass.mapProperties;    NSDictionary *jsonClasses = objectClass.mapClasses;    for (NSString *key in jsonKeys.allKeys) {        NSString *objectProperty = jsonKeys[key];        NSString *value = dict[key];        if (value) {            id klass = jsonClasses[key];            if (!klass) {                [entity setValue:value forKey:objectProperty];            } else if (klass == klass.class) {                [entity setValue:[self decodeRawObject:value intoObjectOfType:klass]                          forKey:objectProperty];            } else if (/* check for containers and blocks */) {                // ...            }        }    }    // ...}


I tried the same approach also. My main problem was, to describe the types accurately. For example for nested arrays I used something like this:

@{ @"friends" : @[[Person class]] }

But things became messy, because I needed to invent more and more special syntax for different types, that I wanted to support. In the end I stopped pursuing this approach and went for another solution - also because the runtime checks I needed to do were slowing the transformation down.

There are a lot of solutions out there right now. I would suggest to have a look at Awesome iOS website. And I also would like to point out JSON Class Generator, because it allows you to

  • describe your types accurately (special mappers for date etc. are included),
  • the types are automatically checked (mismatches reported and fallback values provided),
  • you don't need to spend time for writing repetitive code (and the generated code seems to always "just work")

I hate to advertise, but this tool saved me a lot of work, that I invested into other features of my apps. It came out a little late, now that the world seems to switch to Swift, but even more reason to not waste time on writing models in Objective-C.

Here are some code examples converting to/form JSON:

// converting MyClass <-> NSDictionaryMyClass       *myClass = [MyClass myClassWithDict:someJsonDictionary];NSDictionary *jsonDict = [myClass toDict];// converting NSDictionary <-> NSDataNSDictionary *someJsonDictionary = [NSDictionary api_dictionaryWithJson:someJsonData];NSData *jsonData = [someJsonDictionary api_toJson];

The features mentioned in a previous comment are also supported by JSON Class Generator:

-isEqual, -hash, -description, -copy, NSCoding/NSSecureCoding/NSKeyedArchiver

support for free and does check for missing/additional/NSNull/optional fields and wrong types in the JSON response, reports errors, gracefully fails with fallback values and does this using method dispatch rather than runtime type introspection (which is faster).