Object Mapping library from JSON to NSObjects

前端 未结 5 1925
别那么骄傲
别那么骄傲 2021-02-04 08:39

I am trying to build a parser/objectMapper that will build Objective C objects for the JSON I consume from a REST service.

I took some inspiration from RestKit by having

相关标签:
5条回答
  • 2021-02-04 08:58

    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 */) {
                    // ...
                }
            }
        }
        // ...
    }
    
    0 讨论(0)
  • 2021-02-04 09:05

    Try out CSMapper, it is amazing! You just create plists named the same as the class, map some properties, then you can map a dictionary to your objects with ease with a single line of code. I have tested it on many projects and found it to be very clean, performant. It gave me flexibility to respond to API changes during the development lifecycle with ease.

    https://github.com/marcammann/CSMapper

    I am currently updating the documentation and adding on to the project on a personal fork which should hopefully be merged soon :)

    https://github.com/AntonTheDev/CSMapper

    0 讨论(0)
  • 2021-02-04 09:10

    My recommendation is to use the Motis category on NSObject. It is light-weight, performs automatic validation to your object types and very simple to use.

    There is an exapmle in this other question: Mapping JSON objects in custom objects

    Hope it is useful.

    0 讨论(0)
  • 2021-02-04 09:13

    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.

    0 讨论(0)
  • 2021-02-04 09:19

    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 <-> NSDictionary
    MyClass       *myClass = [MyClass myClassWithDict:someJsonDictionary];
    NSDictionary *jsonDict = [myClass toDict];
    
    // converting NSDictionary <-> NSData
    NSDictionary *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).

    0 讨论(0)
提交回复
热议问题