Use of the SYMFONY2 Framework Serialization tool (i)

Source: Internet
Author: User
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.

  • Related Article

    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.