0

I have two classes

  • Author with attributes id, papers (Paper relationship), ...
  • Paper with attributes id, mainAuthor (Author relationship), authors (Author relationship) ...

and want to map some JSON to it

"authors": [
    {
        "id": 123,
        "papers": [
            {
                "id": 1,
                "main_author_id": 123,
                ...
            },
            ...
        ]
    },
    ...
]

The problem is that the JSON is not structured like

"authors": [
    {
        "id": 123,
        "papers": [
            {
                "id": 1,
                "main_author": {
                    "id": 123
                }
                ...
            },
            ...
        ]
    },
    ...
]

so that I could easily map things (note the *main_author* part of both JSON examples). I tried using mapping this value without a key path as explained here:

[authorMapping addAttributeMappingToKeyOfRepresentationFromAttribute:@"main_author_id"];
[authorMapping addAttributeMappingsFromDictionary:@{@"(main_author_id)": @"id"}];

but I'm getting an error telling me that the keyPath id already exists and I may not add a second mapping for this keyPath. I totally understand this error, but I have no idea how to map from *main_author_id* back to id. Changing the data source may be the best solution, but this is unfortunately not possible.

Any suggestion is highly welcome! Thanks!

tilo
  • 14,009
  • 6
  • 68
  • 85
  • 1
    Have you looked at foreign key mapping to setup the relationship? – Wain Jul 18 '13 at 09:37
  • @Wain this is brilliant, didn't think about that before! Feel free to post this as an answer and I'm happy to accept this as an answer! – tilo Jul 18 '13 at 10:16

2 Answers2

1

This is exactly what foreign key mapping is for. It allows you to temporarily store the 'identity' value that you're provided with and then RestKit will find the appropriate object and complete the relationship.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

Apart from the Answer @Wain (foreign key mapping) provided, it is also possible to implement a custom serialization (c.f. RKSerialization) and modify the objects before mapping takes place. However, the aforementioned method is superior to this (somehow ugly) solution.

tilo
  • 14,009
  • 6
  • 68
  • 85