I do not know which version to start with (I'm using 2.8) to start with the serialization tool (you can not install Jmsserializer), but in addition to the Config.yml file in the framework of an option, no longer see any information to explain how to use it. Symfony2 text block writing speed can't keep up with the speed of the function is always a problem, there is no way you can only from the code to study exactly what kind of usage. Here are some conclusions to be shared with you.
Among the CONFIG.YML:
Framework: Serializer: ~
This will open a service called serializer, usage:
If there is a $user object in a controller action: $json = $this->get (' serializer ')->serialize ($user, ' json ');// Or you can just normalize into an array $userdata = $this->get (' serializer ')->normalize ($user);
In general, the JSON data key is snake_case style, and the default serialize out is CamelCase, of course, the framework of the developers are ready, we need to do is to modify the configuration:
Framework: Serializer: name_converter:serializer.name_converter.camel_case_to_snake_case
Symfony2 even provides caching options, and there are already relevant configurations in the CONFIG_PROD.YML, except that they are commented out by default:
Framework: Serializer: CACHE:SERIALIZER.MAPPING.CACHE.APC
If you have seen the Symfony serializer Component document, you should know what the serialization component supports to define group, what does it mean? For example, the following code:
Use symfony\component\serializer\annotation\groups; Class user{ /** * @Groups ({"Group1"}) */public $name; ...}
If the serialization is using the $serializer->normalize ($user, ' json ', [' groups ' = ' group1 ']) to specify only the properties of the group1, then only the name Properties are exposed to the results of the interface.
Specifies that group can use YAML and XML as well as annotation, whereas annotation is turned off by default, but we can open it with the following configuration:
Framework: Serializer: enable_annotations:true
This article simply describes the correct way to open the Symfony2 Framework serialization tool, but the serialization work is much more than that. The framework uses Objectnormalizer by default to get the property values of an object (Objectnormalizer uses Symfony propertyaccess Component), which can work well without special requirements. The next chapter will show you how to use Getsetmethodnormalizer, that is, how to expose object property values only through the getter/hasser/isser of objects, and how to add more custom normalizer.