-1

My question is very similar to RestKit: mapping JSON array of strings, except it is specific to RestKit 0.09. I have looked at several other questions but am still stuck.

For reasons beyond my control I cannot upgrade to RestKit 0.20. The JSON I'm working with is:

{
   "brands":[
      "AN",
      "UO",
      "FP",
      "TR",
      "BN"
   ],
   "meta":{
      "message":"Current Registry Configuration."
   },
   "event_types":[
      "WEDDING",
      "ANNIVERSARY",
      "BIRTHDAY",
      "HOUSEWARMING",
      "SPECIAL OCCASION"
   ],
   "links":[

  ]
}

I am able to map the "meta" (and several other domain) objects just fine. But I have not been able to map "event_types", and have no need for "brands" or "links".

My current code is as follows:

+ (void) addElementMappings:(RKObjectMapping *)mapping
{
    if ([[self superclass] respondsToSelector:@selector(addElementMappings:)]) {
        [[self superclass] performSelector:@selector(addElementMappings:)
                                withObject:mapping];
    }
    [mapping mapKeyPath:@"event_types"
            toAttribute:@"eventType"];

    RKObjectManager* objectManager = [RKObjectManager sharedManager];
    if (![objectManager.mappingProvider mappingForKeyPath:@"event_types"]) {
        [objectManager.mappingProvider setMapping:mapping
                                       forKeyPath:@"event_types"]; // for KVC path
    }
}

and eventType is a NSArray (I've also tried defining it as NSString - same results as below).

As is, the code throws an exception "this class is not key value coding-compliant for the key event_types."

If I change the mapKeyPath from @"event_types" to nil (similar to 0.20), I get an exception "Cannot define an element mapping an element name to map from".

If I omit the [mapping mapKeyPath:toAttribute] entirely, there are no exceptions but of course I only get the meta object, not event_types.

In RestKit 0.09, how do I map attributes without keys?

Community
  • 1
  • 1
Kevin OMara
  • 323
  • 5
  • 15

1 Answers1

0

The solution was to create a class (GRConfigDTO) that contained the nested objects of interest (brands, event_types) as NSArray. The mapping was simple:

[mapping mapKeyPath:@"brands"
        toAttribute:@"brands"];
[mapping mapKeyPath:@"event_types"
        toAttribute:@"eventTypes"];

My predecessors had modified RestKit .09 (and our server JSON) to ensure that mapping was never ambiguous and therefore always used nil targetClass - which is why upgrading RestKit was not an option. I was working with a 3rd party service and had to deal with their JSON as-is.

The key to making it all work was specifying the targetClass in the sendObject call:

    [self sendObject:nil
                path:kConfig
         requestType:GET
         targetClass:[GRConfigDTO class]
            delegate:self
      callbackMethod:@selector(registryEventTypesResults:error:)];

With the targetClass set, RestKit was able to perform the mapping as expected.

Kevin OMara
  • 323
  • 5
  • 15