如何使用序列化(Serializer)
一個(gè)對(duì)象和不同格式(比如JSON或XML)之間的序列化和反序列化是一個(gè)非常復(fù)雜的話題。Symfony自帶了一個(gè) Serializer組件,給你提供了一些工具,可以隨需利用。
實(shí)際上,在你開始使用之前,先熟悉一下serializer,normalizer和encoder,閱讀 Serializer組件 一文。
激活Serializer ?
serializer
服務(wù)默認(rèn)不可用。要開啟它,在你的配置文件中激活它:
PHP:// app/config/config.php$container->loadFromExtension('framework', array( // ... 'serializer' => array( 'enabled' => true, ),));
XML:<!-- app/config/config.xml --><?xml version="1.0" encoding="UTF-8" ?><container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:framework="http://symfony.com/schema/dic/symfony" xmlns:twig="http://symfony.com/schema/dic/twig" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd"> <framework:config> <!-- ... --> <framework:serializer enabled="true" /> </framework:config></container>
YAML:# app/config/config.ymlframework: # ... serializer: enabled: true
使用Serializer服務(wù) ?
一旦開啟, serializer
服務(wù)可以在你需要它時(shí)注入到任何服務(wù)中,或者像下面這樣在控制器中使用:
// src/AppBundle/Controller/DefaultController.phpnamespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class DefaultController extends Controller{ public function indexAction() { $serializer = $this->get('serializer'); // ... }}
添加Normalizers和Encoders ?
被開啟之后, serializer
在容器中可用,被加載時(shí)還帶有兩個(gè) encoders(JsonEncoder
和 XmlEncoder
)以及ObjectNormalizer normalizer。
你可以加載normalizers和/或encoders,只要給它們打上 serializer.normalizer 和 serializer.encoder 標(biāo)簽。也可以設(shè)置標(biāo)簽的優(yōu)先級(jí),以便決定匹配順序。
下例演示了如何加載 GetSetMethodNormalizer
:
PHP:// app/config/services.phpuse Symfony\Component\DependencyInjection\Definition; $definition = new Definition( 'Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer'));$definition->setPublic(false);$definition->addTag('serializer.normalizer');$container->setDefinition('get_set_method_normalizer', $definition);
XML:<!-- app/config/services.xml --><services> <service id="get_set_method_normalizer" class="Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer" public="false"> <tag name="serializer.normalizer" /> </service></services>
YAML:# app/config/services.ymlservices: get_set_method_normalizer: class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer public: false tags: - { name: serializer.normalizer }
使用序列化的群組Annotations ?
通過以下配置來開啟 序列化群組注釋:
PHP:// app/config/config.php$container->loadFromExtension('framework', array( // ... 'serializer' => array( 'enable_annotations' => true, ),));
XML:<!-- app/config/config.xml --><framework:config> <!-- ... --> <framework:serializer enable-annotations="true" /></framework:config>
YAML:# app/config/config.ymlframework: # ... serializer: enable_annotations: true
接下來,添加 @Goups annotations 到你的類中,然后在序列化時(shí),選擇要使用哪個(gè)群組:
$serializer = $this->get('serializer');$json = $serializer->serialize( $someObject, 'json', array('groups' => array('group1')));
除了 @Groups
注釋,serializer組件還支持Yaml或Xml文件。這些組件在存放于以下位置時(shí)將被自動(dòng)加載:
存放在bundle下面的
Resources/config/
目錄中的serialization.yml
或serialization.xml
;存放在bundle下面的
Resources/config/serialization/
目錄中的所有*.yml
和*.xml
。
開啟Metadata緩存 ?
被Serializer組件所使用的metadata,諸如groups,可以被緩存,以提升程序性能。任何實(shí)現(xiàn)了 Doctrine\Common\Cache\Cache
接口的服務(wù)都可以被使用。
一個(gè)利用了 APCu 的服務(wù)被內(nèi)置在框架中:
PHP:// app/config/config_prod.php$container->loadFromExtension('framework', array( // ... 'serializer' => array( 'cache' => 'serializer.mapping.cache.apc', ),));
XML:<!-- app/config/config_prod.xml --><framework:config> <!-- ... --> <framework:serializer cache="serializer.mapping.cache.apc" /></framework:config>
YAML:# app/config/config_prod.ymlframework: # ... serializer: cache: serializer.mapping.cache.apc
開啟一個(gè)命名轉(zhuǎn)換器 ?
2.8 name_converter
選項(xiàng)從Symfony 2.8開始被引入。
要使用 name converter服務(wù),可以在配置文件中使用 name_converter選項(xiàng)進(jìn)行定義。
內(nèi)置的 CamelCase to snake_case name converter(駝峰轉(zhuǎn)蛇型轉(zhuǎn)換器)可以通過設(shè)置 serializer.name_converter.camel_case_to_snake_case
選項(xiàng)值來開啟:
PHP:// app/config/config.php$container->loadFromExtension('framework', array( // ... 'serializer' => array( 'name_converter' => 'serializer.name_converter.camel_case_to_snake_case, ), ));
XML:<!-- app/config/config.xml --><framework:config> <!-- ... --> <framework:serializer name-converter="serializer.name_converter.camel_case_to_snake_case" /></framework:config>
YAML:# app/config/config.ymlframework: # ... serializer: name_converter: 'serializer.name_converter.camel_case_to_snake_case'
深入Serializer ?
ApiPlatform 提供了一個(gè)API系統(tǒng),它支持 JSON-LD 和 Hydra Core Vocabulary hypermedia格式。它是基于Symfony框架和Serializer組件而構(gòu)建的。它提供了自定義的normalizers,一個(gè)自定義的encoder,自定義的metadata以及一個(gè)緩存系統(tǒng)。
如果你希望利用好Symfony Serializer組件的全部威力,應(yīng)該看一看這個(gè)bundle是如何工作的。