Use of the Symfony2 frame serialization tool (II)-php Tutorial

Source: Internet
Author: User
Tags normalizer
Use of the Symfony2 frame serialization tool (2) ObjectNormalizer is used by default in the Symfony2 framework, as mentioned in the previous article. However, in addition to the default method, you can also have more options. for example, if you use GetSetMethodNormalizer, you only need to register a service:

services:    app.serializer.method_normalizer:        class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer        tags:            - { name: serializer.normalizer }

The key to defining a service as Normalizer is the definition of tags. As long as a service is defined as serializer. normalizer, it becomes a Normalizer in the serializer service.

The GetSetMethodNormalizer constructor allows you to ignore any parameters. However, you cannot use Annotation to define the Serialization Group mentioned in the previous article. In addition, when you register a new Normalizer, config. the name_converter in yml does not work either (this setting only works for the Normalizer that comes with the framework). Therefore, when registering this Normalizer service, it is best to imitate the Normalizer that comes with the framework, read the @ serializer configured by Annotation. mapping. class_metadata_factory service and @ serializer. name_converter.camel_case_to_snke_case is injected into the service:

services:    app.serializer.method_normalizer:        class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer        arguments:            - '@serializer.mapping.class_metadata_factory'            - '@serializer.name_converter.camel_case_to_snake_case'        tags:            - { name: serializer.normalizer }

Circular Reference Handler

Serialization is a complex topic mentioned in the Symfony2 document. for example, you must be careful when dealing with circular references. for example, if a User has a Group, however, there are n users in the Group. if such a User object is serialized, the Group object of the User will be automatically serialized. when the Group object is serialized, all User objects in the Group will be automatically serialized ...... Fortunately, the serialization tool of Symfony2 is configurable.

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.