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.