dchohfi / KeyValueObjectMapping

Automatic KeyValue Object Mapping for Objective-C, parse JSON/plist/Dictionary automatically
http://dchohfi.com/
MIT License
601 stars 90 forks source link

Newbie with the pod #71

Open halbano opened 9 years ago

halbano commented 9 years ago

Hi all,

I'm having problems to parse a JSON Structure like that:

"data": [
{
  "_id": "5570d8520cf28e92e8f26d1c",
  "imageUrl": "http://bucket.konacloud.io/external/api/bucket/null/papermoon/moons/kf-c30a740a-1fde-4d7e-9b57-ff3a3597192e",
  "photoCoordinates": {
    "topLeft": {
      "x": 246.84910383779814,
      "y": 198.83961701323295
    },
    "topRight": {
      "x": 545.3495307916241,
      "y": 247.7891994494284
    },
    "bottomLeft": {
      "x": 213.34832193248192,
      "y": 403.13140469208895
    },
    "bottomRight": {
      "x": 511.84874888630793,
      "y": 452.0809871282844
    }
  },
  "textCoordinates": {
    "topLeft": {
      "x": 241.8624242050808,
      "y": 8.429022588484031
    },
    "topRight": {
      "x": 544.3380331880693,
      "y": 11.088436558626118
    },
    "bottomLeft": {
      "x": 241.42265371284054,
      "y": 58.44750370747054
    },
    "bottomRight": {
      "x": 543.898262695829,
      "y": 61.10691767761263
    }
  },
  "metadata": {
    "text": {
      "left": 241.86242420508074,
      "top": 8.429022588484031,
      "angle": 0.5037406847670752,
      "width": 302.48729975371583,
      "height": 50.020414347906545
    },
    "photo": {
      "left": 246.8491038377981,
      "top": 198.83961701323295,
      "angle": 9.312760096851482,
      "width": 302.48729975371583,
      "height": 207.020377985575
    }
  },
  "category": {
    "_id": "556cedaf0cf2bfc0f5ba1820",
    "name": "Easter",
    "description": "Easter Moons"
  }
}

]

I have a Moon class defined in this way:

@interface Moon : NSManagedObject

@property (nonatomic, retain) NSNumber * moonId;
@property (nonatomic, retain) NSString * imageUrl;
@property (nonatomic, retain) PMCategory *category;
@property (nonatomic, retain) Coordinates *photoCoordinates;
@property (nonatomic, retain) Coordinates *textCoordinates;

@end

And a the corresponding DCParserConfiguration setup for this class is:

-(DCParserConfiguration*) getMoonsParserConfiguration{

DCParserConfiguration *config = [DCParserConfiguration configuration];

DCObjectMapping *mapId = [DCObjectMapping mapKeyPath:@"moonId" toAttribute:@"_id" onClass:[Moon class]];
DCObjectMapping *mapUrl = [DCObjectMapping mapKeyPath:@"imageUrl" toAttribute:@"imageUrl" onClass:[Moon class]];

DCObjectMapping *categoryToMoon = [DCObjectMapping mapKeyPath:@"category" toAttribute:@"category" onClass:[PMCategory class]];

DCArrayMapping *mapperPhotoCoordinates = [DCArrayMapping mapperForClassElements:[Coordinates class] forAttribute:@"photoCoordinates" onClass:[Moon class]];
DCArrayMapping *mapperTextCoordinates = [DCArrayMapping mapperForClassElements:[Coordinates class] forAttribute:@"textCoordinates" onClass:[Moon class]];
DCArrayMapping *mapper = [DCArrayMapping mapperForClassElements:[PMCategory class] forAttribute:@"category" onClass:[Moon class]];

[config addObjectMapping:mapId];
[config addObjectMapping:mapUrl];
[config addObjectMapping:categoryToMoon];
[config addArrayMapper:mapperPhotoCoordinates];
[config addArrayMapper:mapperTextCoordinates];
[config addArrayMapper:mapper];

return config;
}

I've added the following line to config trying to fix the problem, but I think that shouldn't be there.

 DCObjectMapping *mapUrl = [DCObjectMapping mapKeyPath:@"imageUrl" toAttribute:@"imageUrl" onClass:[Moon class]];

This line is confusing for me, I imagine on my first approach to this library that the mapKeyPath is the server response JSON key and the attribute is the Model attribute, but seems to be that they must go swapped?

 DCObjectMapping *mapId = [DCObjectMapping mapKeyPath:@"moonId" toAttribute:@"_id" onClass:[Moon class]];

Otherwise I have an error validating the key into the DCAttributeSetter class implementation, the line that validates the value fails with the first attribute If the line above is not present. Anyway I couldn't solve the problem at all, now with the second attribute imageUrl (named identically on the hash) is failing on the same line.

if([object validateValue:&value forKey:attributeName error:nil]){

I'm really stuck with this and any help to try to solve my issues is really appreciated. Best, Hernan