How to achieve model side-loading with JMS Serializer and Symfony2

空扰寡人 提交于 2019-12-04 12:05:47

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

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'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!