Arkcann
Arkcann

Reputation: 668

Flatten request mapping hierarchy in RestKit

With RestKit, I'm trying to create a request mapping that would convert a payment object into JSON. This object stores sub-payments, of which only one is valid. This valid payment is represented by the property selectedPayment. I think the typical approach for this is to use a relationship mapping however I don't want the selectedPayment to appear as a nested key. An example of the result of my current approach is below:

{
  "requestUserId" : "6",
  "payment" : {
    "selectedPayment" : {
      "amount" : "5",
      "id" : 70,
      "type" : "SOME_TYPE"
    }
  }
}

My desired output is the following:

{
  "requestUserId" : "6",
  "payment" : {
    "amount" : "5",
    "id" : 70,
    "type" : "SOME_TYPE"
  }
}

I have tried numerous options for my relationship mapping's destination key path: @root, @parent, "", nil. All of which have resulted in a crash (except "", which nests the selected payment under an empty key).

I could modify the attribute mappings of the request mapping that is used for selectedPayment to explicitly use @root or @parent but this would not be ideal as it would negatively affect other areas that mapping is used.

This seems like typical behaviour, converting a "deep" object representation into a "shallow" JSON representation, surely there is a straightforward way to do it?

Upvotes: 3

Views: 91

Answers (2)

user4307744
user4307744

Reputation:

Instead of using a relationship mapping, use key paths in your single mapping, navigate into the property and then extract the keys that you seeking.

Upvotes: 1

Wain
Wain

Reputation: 119031

Don't use a relationship mapping, instead use key paths in your single mapping to navigate into the property and extract the keys you want. So you would use selectedPayment.amount, selectedPayment.id and selectedPayment.type as the source key paths in the mapping and amount, id and type As the respective destination keys.

Upvotes: 0

Related Questions