3

I'm building a project with Ember.js and Ember-data for the UI and Symfony2, FOSRestBundle and JMS Serializer for the backend JSON API. JMS Serializer always embeds nested models in its output, but Ember-data requires that the models are side-loaded. I can't find anywhere an example of configuring JMS Serializer to side-load models rather than embedding them.

Of course, I could just write an adapter on the Ember-data side to transform the result, but I want to gain the benefits of side-loading data and not just work around a (potential) limitation in JMS Serializer.

This is what I mean by embeded model data, which is what JMS-Serializer does now:

{
  "post": {
    "id": 1,
    "name": "Test Post",
    "comments": [
      {
        "id": 1,
        "comment": "Awesome post, man!"
      }, {
        "id": 2,
        "comment": "Yeah, what he said."
      }
    ]
  }
}

This is what I mean by side-loaded model data, which is what I want:

{
  "post": {
    "id": 1,
    "name": "Test Post",
    "comments": [1, 2]
  },
  "comments": [
    {
      "id": 1,
      "comment": "Awesome post, man!"
    }, {
      "id": 2,
      "comment": "Yeah, what he said."
    }
  ]
}

Does anyone know of a configuration to achieve what I want? Or has anyone implemented this functionality in JMS-Serialiser?

Paul Ferrett
  • 4,090
  • 2
  • 17
  • 19
  • THe adapter's [normalizePayload](http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_normalizePayload) method is provided to enable JSON transformations and would still give you all of the benefits of sideloading. It would require a far amount of transformation code which I guess your trying to avoid. – TrevTheDev Mar 08 '14 at 02:17
  • Thanks for the comment, @TrevTheDev, but I note in the question that this is an option that I'd rather avoid. I also want the benefits of side loading that would come from implementing on the server side. – Paul Ferrett Mar 08 '14 at 03:53
  • If your transformation code transforms the JSON received into equivalent side loading JSON you should still get the equivalent benefits of side loading - I assume your mean having the relationships defined. – TrevTheDev Mar 08 '14 at 05:28
  • My server already loads all models, so you're right that I get that benefit there. One of the other benefits I'm talking about is not sending back duplicate data. E.g. If I send back 100 posts, and they all have the same `Tag` then the data for that tag will be sent back 100 times. – Paul Ferrett Mar 08 '14 at 15:10
  • To prevent that you can override the serialize method of the Serializer and customize exactly what JSON is submitted back to the Server. – TrevTheDev Mar 09 '14 at 10:09

2 Answers2

4

I've implemented a custom JSON Serialization Visitor class that will side-load the data for embedded objects rather than encode them inline. The class can be found on GitHub here.

Example Usage:

$visitor = new SideLoadJsonSerializationVisitor(
    new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()));

$serializer = SerializerBuilder::create()
    ->setSerializationVisitor('json', $visitor)
    ->build();

echo $serializer->serialize(array('myClass' => $myClass), 'json');

Or you can use it in your Symfony2 project by overriding the JSON Serialization Visitor class

parameters:
    jms_serializer.json_serialization_visitor.class: 'Acme\MyBundle\Serializer\SideLoadJsonSerializationVisitor'
Paul Ferrett
  • 4,090
  • 2
  • 17
  • 19
1

There is a bundle which supports some more features like async loading and some more flexible implementing and security functions.

https://github.com/UniqueLibs/ember-data-serializer-bundle

Xraid
  • 26
  • 1
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone May 29 '15 at 13:49
  • The linked bundle was written to serialize php objects and arrays for ember data like the example in the first post. But it provides the functionality to restrict access to each object and you can define which object relations should be loaded asynchronous. You can find more information in the bundle documentation: https://github.com/UniqueLibs/ember-data-serializer-bundle/blob/master/Resources/doc/index.md – Xraid May 30 '15 at 11:48
  • Thank you! This looks like a much better solution :) – Paul Ferrett Jun 02 '15 at 09:18