8000 symfony-docs/serializer.rst at 3.0 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content
{"payload":{"allShortcutsEnabled":false,"fileTree":{"":{"items":[{"name":".github","path":".github","contentType":"directory"},{"name":".platform","path":".platform","contentType":"directory"},{"name":"_build","path":"_build","contentType":"directory"},{"name":"_images","path":"_images","contentType":"directory"},{"name":"_includes","path":"_includes","contentType":"directory"},{"name":"assetic","path":"assetic","contentType":"directory"},{"name":"best_practices","path":"best_practices","contentType":"directory"},{"name":"bundles","path":"bundles","contentType":"directory"},{"name":"components","path":"components","contentType":"directory"},{"name":"configuration","path":"configuration","contentType":"directory"},{"name":"console","path":"console","contentType":"directory"},{"name":"contributing","path":"contributing","contentType":"directory"},{"name":"controller","path":"controller","contentType":"directory"},{"name":"create_framework","path":"create_framework","contentType":"directory"},{"name":"debug","path":"debug","contentType":"directory"},{"name":"deployment","path":"deployment","contentType":"directory"},{"name":"doctrine","path":"doctrine","contentType":"directory"},{"name":"email","path":"email","contentType":"directory"},{"name":"event_dispatcher","path":"event_dispatcher","contentType":"directory"},{"name":"form","path":"form","contentType":"directory"},{"name":"frontend","path":"frontend","contentType":"directory"},{"name":"getting_started","path":"getting_started","contentType":"directory"},{"name":"http_cache","path":"http_cache","contentType":"directory"},{"name":"introduction","path":"introduction","contentType":"directory"},{"name":"logging","path":"logging","contentType":"directory"},{"name":"profiler","path":"profiler","contentType":"directory"},{"name":"quick_tour","path":"quick_tour","contentType":"directory"},{"name":"reference","path":"reference","contentType":"directory"},{"name":"request","path":"request","contentType":"directory"},{"name":"routing","path":"routing","contentType":"directory"},{"name":"security","path":"security","contentType":"directory"},{"name":"serializer","path":"serializer","contentType":"directory"},{"name":"service_container","path":"service_container","contentType":"directory"},{"name":"session","path":"session","contentType":"directory"},{"name":"setup","path":"setup","contentType":"directory"},{"name":"templating","path":"templating","contentType":"directory"},{"name":"testing","path":"testing","contentType":"directory"},{"name":"translation","path":"translation","contentType":"directory"},{"name":"validation","path":"validation","contentType":"directory"},{"name":".editorconfig","path":".editorconfig","contentType":"file"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":".platform.app.yaml","path":".platform.app.yaml","contentType":"file"},{"name":".travis.yml","path":".travis.yml","contentType":"file"},{"name":"README.markdown","path":"README.markdown","contentType":"file"},{"name":"assetic.rst","path":"assetic.rst","contentType":"file"},{"name":"bundles.rst","path":"bundles.rst","contentType":"file"},{"name":"changelog.rst","path":"changelog.rst","contentType":"file"},{"name":"configuration.rst","path":"configuration.rst","contentType":"file"},{"name":"console.rst","path":"console.rst","contentType":"file"},{"name":"controller.rst","path":"controller.rst","contentType":"file"},{"name":"debug.rst","path":"debug.rst","contentType":"file"},{"name":"deployment.rst","path":"deployment.rst","contentType":"file"},{"name":"doctrine.rst","path":"doctrine.rst","contentType":"file"},{"name":"email.rst","path":"email.rst","contentType":"file"},{"name":"event_dispatcher.rst","path":"event_dispatcher.rst","contentType":"file"},{"name":"expressions.rst","path":"expressions.rst","contentType":"file"},{"name":"forms.rst","path":"forms.rst","contentType":"file"},{"name":"frontend.rst","path":"frontend.rst","contentType":"file"},{"name":"http_cache.rst","path":"http_cache.rst","contentType":"file"},{"name":"index.rst","path":"index.rst","contentType":"file"},{"name":"logging.rst","path":"logging.rst","contentType":"file"},{"name":"page_creation.rst","path":"page_creation.rst","contentType":"file"},{"name":"performance.rst","path":"performance.rst","contentType":"file"},{"name":"profiler.rst","path":"profiler.rst","contentType":"file"},{"name":"request.rst","path":"request.rst","contentType":"file"},{"name":"routing.rst","path":"routing.rst","contentType":"file"},{"name":"security.rst","path":"security.rst","contentType":"file"},{"name":"serializer.rst","path":"serializer.rst","contentType":"file"},{"name":"service_container.rst","path":"service_container.rst","contentType":"file"},{"name":"session.rst","path":"session.rst","contentType":"file"},{"name":"setup.rst","path":"setup.rst","contentType":"file"},{"name":"templating.rst","path":"templating.rst","contentType":"file"},{"name":"testing.rst","path":"testing.rst","contentType":"file"},{"name":"translation.rst","path":"translation.rst","contentType":"file"},{"name":"validation.rst","path":"validation.rst","contentType":"file"}],"totalCount":75}},"fileTreeProcessingTime":5.611685,"foldersToFetch":[],"incompleteFileTree":false,"repo":{"id":521583,"defaultBranch":"7.3","name":"symfony-docs","ownerLogin":"symfony","currentUserCanPush":false,"isFork":false,"isEmpty":false,"createdAt":"2010-02-17T08:43:51.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/143937?v=4","public":true,"private":false,"isOrgOwned":true},"codeLineWrapEnabled":false,"symbolsExpanded":false,"treeExpanded":true,"refInfo":{"name":"3.0","listCacheKey":"v0:1751870253.0","canEdit":false,"refType":"branch","currentOid":"fdadad00a2d72106512b155f462aab1a86f2eaeb"},"path":"serializer.rst","currentUser":null,"blob":{"rawLines":null,"stylingDirectives":null,"colorizedLines":null,"csv":null,"csvError":null,"dependabotInfo":{"showConfigurationBanner":false,"configFilePath":null,"networkDependabotPath":"/symfony/symfony-docs/network/updates","dismissConfigurationNoticePath":"/settings/dismiss-notice/dependabot_configuration_notice","configurationNoticeDismissed":null},"displayName":"serializer.rst","displayUrl":"https://github.com/symfony/symfony-docs/blob/3.0/serializer.rst?raw=true","headerInfo":{"blobSize":"6.94 KB","deleteTooltip":"You must be signed in to make or propose changes","editTooltip":"You must be signed in to make or propose changes","ghDesktopPath":"https://desktop.github.com","isGitLfs":false,"onBranch":true,"shortPath":"f3c39bb","siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fsymfony%2Fsymfony-docs%2Fblob%2F3.0%2Fserializer.rst","isCSV":false,"isRichtext":true,"toc":[{"level":2,"text":"How to Use the Serializer","anchor":"how-to-use-the-serializer","htmlText":"How to Use the Serializer"},{"level":3,"text":"Activating the Serializer","anchor":"activating-the-serializer","htmlText":"Activating the Serializer"},{"level":3,"text":"Using the Serializer Service","anchor":"using-the-serializer-service","htmlText":"Using the Serializer Service"},{"level":3,"text":"Adding Normalizers and Encoders","anchor":"adding-normalizers-and-encoders","htmlText":"Adding Normalizers and Encoders"},{"level":3,"text":"Using Serialization Groups Annotations","anchor":"using-serialization-groups-annotations","htmlText":"Using Serialization Groups Annotations"},{"level":3,"text":"Enabling the Metadata Cache","anchor":"enabling-the-metadata-cache","htmlText":"Enabling the Metadata Cache"},{"level":3,"text":"Going Further with the Serializer","anchor":"going-further-with-the-serializer","htmlText":"Going Further with the Serializer"}],"lineInfo":{"truncatedLoc":"233","truncatedSloc":"172"},"mode":"file"},"image":false,"isCodeownersFile":null,"isPlain":false,"isValidLegacyIssueTemplate":false,"issueTemplate":null,"discussionTemplate":null,"language":"reStructuredText","languageID":419,"large":false,"planSupportInfo":{"repoIsFork":null,"repoOwnedByCurrentUser":null,"requestFullPath":"/symfony/symfony-docs/blob/3.0/serializer.rst","showFreeOrgGatedFeatureMessage":null,"showPlanSupportBanner":null,"upgradeDataAttributes":null,"upgradePath":null},"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_dockerfile","releasePath":"/symfony/symfony-docs/releases/new?marketplace=true","showPublishActionBanner":false},"rawBlobUrl":"https://github.com/symfony/symfony-docs/raw/refs/heads/3.0/serializer.rst","renderImageOrRaw":false,"richText":"\u003carticle class=\"markdown-body entry-content container-lg\" itemprop=\"text\"\u003e\u003cpre\u003e.. index::\n single: Serializer\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-how-to-use-the-serializer\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eHow to Use the Serializer\u003c/h2\u003e\u003ca id=\"user-content-how-to-use-the-serializer\" class=\"anchor\" aria-label=\"Permalink: How to Use the Serializer\" href=\"#how-to-use-the-serializer\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eSerializing and deserializing to and from objects and different formats (e.g.\nJSON or XML) is a very complex topic. Symfony comes with a\n\u003ca href=\"#id1\"\u003e\u003cspan id=\"user-content-id2\"\u003e:doc:`Serializer Component \u0026lt;/components/serializer\u0026gt;`\u003c/span\u003e\u003c/a\u003e, which gives you some\ntools that you can leverage for your solution.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eIn fact, before you start, get familiar with the serializer, normalizers\nand encoders by reading the \u003ca href=\"#id3\"\u003e\u003cspan id=\"user-content-id4\"\u003e:doc:`Serializer Component \u0026lt;/components/serializer\u0026gt;`\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\n\u003ca name=\"user-content-activating-the-serializer\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eActivating the Serializer\u003c/h3\u003e\u003ca id=\"user-content-activating-the-serializer\" class=\"anchor\" aria-label=\"Permalink: Activating the Serializer\" href=\"#activating-the-serializer\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe \u003ccode\u003eserializer\u003c/code\u003e service is not available by default. To turn it on, activate\nit in your configuration:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: yaml\n\n # app/config/config.yml\n framework:\n # ...\n serializer:\n enabled: true\n\n .. code-block:: xml\n\n \u0026lt;!-- app/config/config.xml --\u0026gt;\n \u0026lt;framework:config\u0026gt;\n \u0026lt;!-- ... --\u0026gt;\n \u0026lt;framework:serializer enabled=\"true\" /\u0026gt;\n \u0026lt;/framework:config\u0026gt;\n\n .. code-block:: php\n\n // app/config/config.php\n $container-\u0026gt;loadFromExtension('framework', array(\n // ...\n 'serializer' =\u0026gt; array(\n 'enabled' =\u0026gt; true,\n ),\n ));\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-using-the-serializer-service\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eUsing the Serializer Service\u003c/h3\u003e\u003ca id=\"user-content-using-the-serializer-service\" class=\"anchor\" aria-label=\"Permalink: Using the Serializer Service\" href=\"#using-the-serializer-service\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eOnce enabled, the \u003ccode\u003eserializer\u003c/code\u003e service can be injected in any service where\nyou need it or it can be used in a controller like the following:\u003c/p\u003e\n\u003cpre\u003e// src/AppBundle/Controller/DefaultController.php\nnamespace AppBundle\\Controller;\n\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller;\n\nclass DefaultController extends Controller\n{\n public function indexAction()\n {\n $serializer = $this-\u0026gt;get('serializer');\n\n // ...\n }\n}\n\u003c/pre\u003e\n\u003ca name=\"user-content-adding-normalizers-and-encoders\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eAdding Normalizers and Encoders\u003c/h3\u003e\u003ca id=\"user-content-adding-normalizers-and-encoders\" class=\"anchor\" aria-label=\"Permalink: Adding Normalizers and Encoders\" href=\"#adding-normalizers-and-encoders\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eOnce enabled, the \u003ccode\u003eserializer\u003c/code\u003e service will be available in the container\nand will be loaded with two \u003ca href=\"#id5\"\u003e\u003cspan id=\"user-content-id6\"\u003e:ref:`encoders \u0026lt;component-serializer-encoders\u0026gt;`\u003c/span\u003e\u003c/a\u003e\n(\u003ca href=\"#id7\"\u003e\u003cspan id=\"user-content-id8\"\u003e:class:`Symfony\\\\Component\\\\Serializer\\\\Encoder\\\\JsonEncoder`\u003c/span\u003e\u003c/a\u003e and\n\u003ca href=\"#id9\"\u003e\u003cspan id=\"user-content-id10\"\u003e:class:`Symfony\\\\Component\\\\Serializer\\\\Encoder\\\\XmlEncoder`\u003c/span\u003e\u003c/a\u003e) and the\n\u003ca href=\"#id11\"\u003e\u003cspan id=\"user-content-id12\"\u003e:ref:`ObjectNormalizer normalizer \u0026lt;component-serializer-normalizers\u0026gt;`\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eYou can load normalizers and/or encoders by tagging them as\n\u003ca href=\"#id13\"\u003e\u003cspan id=\"user-content-id14\"\u003e:ref:`serializer.normalizer \u0026lt;reference-dic-tags-serializer-normalizer\u0026gt;`\u003c/span\u003e\u003c/a\u003e and\n\u003ca href=\"#id15\"\u003e\u003cspan id=\"user-content-id16\"\u003e:ref:`serializer.encoder \u0026lt;reference-dic-tags-serializer-encoder\u0026gt;`\u003c/span\u003e\u003c/a\u003e. It's also\npossible to set the priority of the tag in order to decide the matching order.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eHere is an example on how to load the\n\u003ca href=\"#id17\"\u003e\u003cspan id=\"user-content-id18\"\u003e:class:`Symfony\\\\Component\\\\Serializer\\\\Normalizer\\\\GetSetMethodNormalizer`\u003c/span\u003e\u003c/a\u003e:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: yaml\n\n # app/config/services.yml\n services:\n get_set_method_normalizer:\n class: Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer\n public: false\n tags:\n - { name: serializer.normalizer }\n\n .. code-block:: xml\n\n \u0026lt;!-- app/config/services.xml --\u0026gt;\n \u0026lt;services\u0026gt;\n \u0026lt;service id=\"get_set_method_normalizer\" class=\"Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer\" public=\"false\"\u0026gt;\n \u0026lt;tag name=\"serializer.normalizer\" /\u0026gt;\n \u0026lt;/service\u0026gt;\n \u0026lt;/services\u0026gt;\n\n .. code-block:: php\n\n // app/config/services.php\n use Symfony\\Component\\DependencyInjection\\Definition;\n\n $definition = new Definition(\n 'Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer'\n ));\n $definition-\u0026gt;setPublic(false);\n $definition-\u0026gt;addTag('serializer.normalizer');\n $container-\u0026gt;setDefinition('get_set_method_normalizer', $definition);\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-using-serialization-groups-annotations\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eUsing Serialization Groups Annotations\u003c/h3\u003e\u003ca id=\"user-content-using-serialization-groups-annotations\" class=\"anchor\" aria-label=\"Permalink: Using Serialization Groups Annotations\" href=\"#using-serialization-groups-annotations\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eEnable \u003ca href=\"#id19\"\u003e\u003cspan id=\"user-content-id20\"\u003e:ref:`serialization groups annotation \u0026lt;component-serializer-attributes-groups\u0026gt;`\u003c/span\u003e\u003c/a\u003e\nwith the following configuration:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: yaml\n\n # app/config/config.yml\n framework:\n # ...\n serializer:\n enable_annotations: true\n\n .. code-block:: xml\n\n \u0026lt;!-- app/config/config.xml --\u0026gt;\n \u0026lt;framework:config\u0026gt;\n \u0026lt;!-- ... --\u0026gt;\n \u0026lt;framework:serializer enable-annotations=\"true\" /\u0026gt;\n \u0026lt;/framework:config\u0026gt;\n\n .. code-block:: php\n\n // app/config/config.php\n $container-\u0026gt;loadFromExtension('framework', array(\n // ...\n 'serializer' =\u0026gt; array(\n 'enable_annotations' =\u0026gt; true,\n ),\n ));\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eNext, add the \u003ca href=\"#id21\"\u003e\u003cspan id=\"user-content-id22\"\u003e:ref:`@Groups annotations \u0026lt;component-serializer-attributes-groups-annotations\u0026gt;`\u003c/span\u003e\u003c/a\u003e\nto your class and choose which groups to use when serializing:\u003c/p\u003e\n\u003cpre\u003e$serializer = $this-\u0026gt;get('serializer');\n$json = $serializer-\u0026gt;serialize(\n $someObject,\n 'json', array('groups' =\u0026gt; array('group1'))\n);\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eIn addition to the \u003ccode\u003e@Groups\u003c/code\u003e annotation, the Serializer component also\nsupports Yaml or XML files. These files are automatically loaded when being\nstored in one of the following locations:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eThe \u003ccode\u003eserialization.yml\u003c/code\u003e or \u003ccode\u003eserialization.xml\u003c/code\u003e file in\nthe \u003ccode\u003eResources/config/\u003c/code\u003e directory of a bundle;\u003c/li\u003e\n\u003cli\u003eAll \u003ccode\u003e*.yml\u003c/code\u003e and \u003ccode\u003e*.xml\u003c/code\u003e files in the \u003ccode\u003eResources/config/serialization/\u003c/code\u003e\ndirectory of a bundle.\u003c/li\u003e\n\u003c/ul\u003e\n\u003ca name=\"user-content-enabling-the-metadata-cache\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eEnabling the Metadata Cache\u003c/h3\u003e\u003ca id=\"user-content-enabling-the-metadata-cache\" class=\"anchor\" aria-label=\"Permalink: Enabling the Metadata Cache\" href=\"#enabling-the-metadata-cache\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eMetadata used by the Serializer component such as groups can be cached to\nenhance application performance. Any service implementing the \u003ccode\u003eDoctrine\\Common\\Cache\\Cache\u003c/code\u003e\ninterface can be used.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eA service leveraging \u003ca href=\"https://github.com/krakjoe/apcu\"\u003eAPCu\u003c/a\u003e (and APC for PHP \u0026lt; 5.5) is built-in.\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: yaml\n\n # app/config/config_prod.yml\n framework:\n # ...\n serializer:\n cache: serializer.mapping.cache.apc\n\n .. code-block:: xml\n\n \u0026lt;!-- app/config/config_prod.xml --\u0026gt;\n \u0026lt;framework:config\u0026gt;\n \u0026lt;!-- ... --\u0026gt;\n \u0026lt;framework:serializer cache=\"serializer.mapping.cache.apc\" /\u0026gt;\n \u0026lt;/framework:config\u0026gt;\n\n .. code-block:: php\n\n // app/config/config_prod.php\n $container-\u0026gt;loadFromExtension('framework', array(\n // ...\n 'serializer' =\u0026gt; array(\n 'cache' =\u0026gt; 'serializer.mapping.cache.apc',\n ),\n ));\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-going-further-with-the-serializer\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eGoing Further with the Serializer\u003c/h3\u003e\u003ca id=\"user-content-going-further-with-the-serializer\" class=\"anchor\" aria-label=\"Permalink: Going Further with the Serializer\" href=\"#going-further-with-the-serializer\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e\u003ca href=\"https://github.com/api-platform/core\"\u003eApiPlatform\u003c/a\u003e provides an API system supporting \u003ca href=\"http://json-ld.org\" rel=\"nofollow\"\u003eJSON-LD\u003c/a\u003e and \u003ca href=\"http://hydra-cg.com\" rel=\"nofollow\"\u003eHydra Core Vocabulary\u003c/a\u003e\nhypermedia formats. It is built on top of the Symfony Framework and its Serializer\ncomponent. It provides custom normalizers and a custom encoder, custom metadata\nand a caching system.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eIf you want to leverage the full power of the Symfony Serializer component,\ntake a look at how this bundle works.\u003c/p\u003e\n\u003cpre\u003e.. toctree::\n :maxdepth: 1\n :glob:\n\n serializer/*\n\n\u003c/pre\u003e\n\n\u003c/article\u003e","renderedFileInfo":null,"shortPath":null,"symbolsEnabled":true,"tabSize":8,"topBannersInfo":{"overridingGlobalFundingFile":false,"globalPreferredFundingPath":"/symfony/.github/blob/6f2ca452c856184a28812bb364b4e34ed50309da/FUNDING.yml","showInvalidCitationWarning":false,"citationHelpUrl":"https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files","actionsOnboardingTip":null},"truncated":false,"viewable":true,"workflowRedirectUrl":null,"symbols":null},"copilotInfo":null,"copilotAccessAllowed":false,"modelsAccessAllowed":false,"modelsRepoIntegrationEnabled":false,"csrf_tokens":{"/symfony/symfony-docs/branches":{"post":"9eWEMysNwDW68xeCEaEGkwBs98TaIL-OG31II-kw61XV1N8QZ_IwEFa_RpUX6Lrr9VzHjSvYibArrLIDYVeYcg"},"/repos/preferences":{"post":"4CCBPAE50jW8eA_Nn141ythseTDPMBwMTOgaHWFDFD6fK0zjeH4ffZ4F8oFs-UQwZP5jkC4PsY_px8ZBg4eSfA"}}},"title":"symfony-docs/serializer.rst at 3.0 · symfony/symfony-docs","appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-263cab1760dd.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-b84e9496fc59.js","githubDevUrl":null,"enabled_features":{"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true}}}
0