Use of the Symfony2 frame serialization tool (1)

Source: Internet
Author: User
Use of the Symfony2 frame serialization tool (1) I don't know which version it is from (I am currently using 2.8) and I have come with the serialization tool (you don't need to install JMSSerializer ), except config. in addition to an option under the framework in the yml file, no information is displayed to illustrate how to use it. It is always a problem that the speed of Symfony2 text block writing cannot catch up with the speed of adding a function. you can't just study the usage of the function from the code. I will share some conclusions here.

In config. yml:

framework:    serializer: ~

In this case, a service called serializer is enabled. usage:

// Assume that there is a $ user object in a controller action: $ json = $ this-> get ('serializer')-> serialize ($ user, 'json '); // or you can just normalize an array $ userData = $ this-> get ('serializer')-> normalize ($ user );

In general, the keys of json data are in the style of "snak_case", while the default serialize is out of "camelCase". of course, the framework developers have prepared for it. All we need to do is modify the configuration:

framework:    serializer:        name_converter: serializer.name_converter.camel_case_to_snake_case

Symfony2 even provides cache options, and related configurations already exist in config_prod.yml, but are commented out by default:

framework:  serializer:      cache: serializer.mapping.cache.apc

If you have read the Symfony Serializer Component document, you should know that the Serialization Component supports defining group. what does it mean? For example, the following code:

use Symfony\Component\Serializer\Annotation\Groups; class User{    /**     * @Groups({"group1"})     */    public $name;     ...}

If $ serializer-> normalize ($ user, 'json', ['group' => ['group1']) is used for serialization, only attributes of group1 are obtained, only the name attribute is exposed in the interface results.

The specified group can use yaml, xml, and annotation. However, annotation is disabled by default, but we can enable it through the following configuration:

framework:    serializer:        enable_annotations: true

This article briefly introduces the correct opening method of the Symfony2 frame serialization tool, but Serialization works far more than that. By default, the framework uses ObjectNormalizer to obtain the property value of an object (ObjectNormalizer uses Symfony PropertyAccess Component). It works well without special requirements. the next chapter will tell you, how to use GetSetMethodNormalizer, that is, to expose object attribute values only through getter, hasser, and isser, and to add more custom Normalizer methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.