4

I'm using FOSRestBundle and JMSSerializerBundle to output JSON data to be consumed by an ember application. I would like to use the built-in adapters of ember-data and the JSON format that is expected has to have a root name. My current JSON is as follows:

{
     "user_id": 1
     "first": "Dan"
     "email": "dan@email.com" 
 }

Ember expects, and what I want is:

{
 "user":
   {
     "user_id": 1
     "first": "Dan"
     "email": "dan@email.com" 
    }
}

User is the name of the entity. I'm pretty sure this is just a configuration thing, but looking through the documentation I can't seem to find what to set. I have tried @XmlRoot, but that doesn't seem to have an affect on the resulting JSON (expected).

Dan
  • 876
  • 5
  • 15

2 Answers2

0

Personally I'd think it's the API consumers job to bend to the API, not the other way around.

I'd suggest taking a look at this example of massaging the payload into a shape ember likes:

Transform JSON to an appropriate format for RESTAdapter EmberJS

Community
  • 1
  • 1
piers.warmers
  • 333
  • 2
  • 6
  • I get that if I was consuming a third party API I would have to create methods to normalize my JSON, but being that I am creating the server API to be consumed by EmberData, I don't see why I shouldn't output the proper JSON from the server. – Dan Jun 27 '14 at 13:58
0

You can do it in the controller which extends FOSRestController by sending an array as the view data, I couldn't find a way to do this in the configuration, but I agree a root node setting would be nice.:

$fooThing = $em->getRepository('Foo')->find($id);
$view = $this->view(array('foo' => $fooThing), 200);
return $this->handleView($view);

I'm pretty sure this is going to mess with my XML, but I'm just starting work on the API so I haven't gotten that far yet.

jrjohnson
  • 2,401
  • 17
  • 23