Ceci est une ancienne révision du document !
jms/serializer-bundle
Installation
D:\webprojects\test-api-project>composer require jms/serializer-bundle Using version ^3.4 for jms/serializer-bundle ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Restricting packages listed in "symfony/symfony" to "4.3.*" Prefetching 16 packages - Downloading (100%) Package operations: 16 installs, 0 updates, 0 removals - Installing hoa/exception (1.17.01.16): Loading from cache - Installing hoa/event (1.17.01.13): Loading from cache - Installing hoa/consistency (1.17.05.02): Loading from cache - Installing hoa/visitor (2.17.01.16): Loading from cache - Installing hoa/ustring (4.17.01.16): Loading from cache - Installing hoa/protocol (1.17.01.14): Loading from cache - Installing hoa/zformat (1.17.01.10): Loading from cache - Installing hoa/iterator (2.17.01.10): Loading from cache - Installing hoa/compiler (3.17.08.08): Loading from cache - Installing hoa/regex (1.17.01.13): Loading from cache - Installing hoa/math (1.17.05.16): Loading from cache - Installing hoa/stream (1.17.02.21): Loading from cache - Installing hoa/file (1.17.07.11): Loading from cache - Installing jms/metadata (2.1.0): Loading from cache - Installing jms/serializer (3.3.0): Loading from cache - Installing jms/serializer-bundle (3.4.1): Loading from cache Writing lock file Generating autoload files ocramius/package-versions: Generating version class... ocramius/package-versions: ...done generating version class Symfony operations: 1 recipe (3bdaac2ac8821bc8af3d710ff51a1686) - WARNING jms/serializer-bundle (>=3.0): From github.com/symfony/recipes-contrib:master The recipe for this package comes from the "contrib" repository, which is open to community contributions. Review the recipe at https://github.com/symfony/recipes-contrib/tree/master/jms/serializer-bundle/3.0 Do you want to execute this recipe? [y] Yes [n] No [a] Yes for all packages, only for the current installation session [p] Yes permanently, never ask again for this project (defaults to n): y - Configuring jms/serializer-bundle (>=3.0): From github.com/symfony/recipes-contrib:master Executing script cache:clear [OK] Executing script assets:install public [OK] Some files may have been created or updated to configure your new packages. Please review, edit and commit them: these files are yours.
Usage
Trois points important
- Clause use :
use JMS\Serializer\SerializerInterface;
- Injection :
public function check(…, SerializerInterface $serializer )
- Sérialisation :
$string = $serializer→serialize($objet, 'json');
<?php namespace App\Controller; ... use JMS\Serializer\SerializerInterface; ... class CheckVersionController extends AbstractController { /** * @Route("/api/checkv", name="api_checkversion") */ public function check(Request $request, SerializerInterface $serializer ) { // Création d'un objet $proj = new Projects(); $proj->setName('Test'); // Sérialisation de l'Objet $data = $serializer->serialize($proj, 'json'); // Envois de la réponse $response = new Response($data); $response->headers->set('Content-Type', 'application/json'); return $response; } }
Vous pourriez laisser un commentaire si vous étiez connecté.