Note that there are some explanatory texts on larger screens.

plurals
  1. POObject Mapping library from JSON to NSObjects
    primarykey
    data
    text
    <p>I am trying to build a parser/objectMapper that will build Objective C objects for the JSON I consume from a REST service.</p> <p>I took some inspiration from RestKit by having my Entities all hold a "decoding list" which tells a mapper which JSON keys goes with which objects. Like this:</p> <pre><code>//ObjectEntity implementation + (NSDictionary*) mapProperties { /* localPropertiy - JSONProperty */ return @{ @"name": @"name", @"category": @"category", @"possible_scopes": @"possibleScopes", @"possible_descriptions": @"possibleDescriptions", @"key": @"keys" }; } + (NSDictionary*) mapRelations { return [NSDictionary dictionary]; } </code></pre> <p>I did so because I like the encapsulation of these changeable values to be in the object that they reference. Making the Mapper know as little as possible.</p> <p>The mapper does something like this:</p> <pre><code>+ (NSArray*) parseData:(NSData*) jsonData intoObjectsOfType:(Class) objectClass { //Parser result from web service NSError *error = nil; CJSONDeserializer *deserializer = [CJSONDeserializer deserializer]; [deserializer setNullObject:nil]; NSArray *objects = [deserializer deserializeAsArray:jsonData error:&amp;error]; NSMutableArray *result = [NSMutableArray array]; for (NSDictionary *o in objects) { id &lt;EntityProtocol&gt; entity = [[objectClass alloc] init]; NSDictionary *jsonKeys = objectClass.mapProperties; for (NSString *key in jsonKeys.allKeys) { NSString *objectProperty = jsonKeys[key]; NSString *value = o[key]; if (value) [entity setValue:value forKey:objectProperty]; } [result addObject:entity]; } return (NSArray*)result; } </code></pre> <p>So I message the parser/mapper like this:</p> <pre><code>NSArray *objects = [ObjectParser parseData:self.responseData intoObjectsOfType:ObjectEntity.class]; </code></pre> <p>This means that the parser must know what my root object is, which is fine as the object retrieving it from the web service of course has this knowledge.</p> <p>The above only works for JSON without nested objects, I have been trying to build the parser so that it will take the relationships into account as well, building the needed objects and inserting them into the root object, this needs to be recursive and I keep running into dead ends.</p> <p>I would like some help to how I could approach this or any insight to as if something like this exists out as a library. Maybe for using or maybe just for tackling the parts I have problems with.</p> <p>Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload