rest - Symfony2 FOSRESTBundle serialization entity array to JSON -
i have problem trying serialize array of entity game in json. have @ begining circular reference exception managed fix (but maybe solution source of problem).
currently request /games return valide json array not correctly displayed in browser , application not manage parse it. chrome return me following error :
resource interpreted document transferred mime type application/json
what surprising send content-type application/json. same headers used request /players , correctly displayed , parsed no error.
i have bidirectionnal relation between 2 entity : game & player.
entity game :
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; /** * game * * @orm\table(name="game") * @orm\entity(repositoryclass="appbundle\repository\gamerepository") */ class game { /** * @var int * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\manytoone(targetentity="appbundle\entity\tournamenttype", inversedby="games") */ private $reftype; /** * @var \datetime * * @orm\column(name="tournamentdate", type="date") */ private $tournamentdate; /** * @orm\manytoone(targetentity="appbundle\entity\ground", inversedby="games") */ private $ground; /** * @orm\manytoone(targetentity="appbundle\entity\referee", inversedby="games") */ private $referee; /** * @orm\manytomany(targetentity="appbundle\entity\player", cascade={"persist"}) */ private $players; /** * @var int * * @orm\column(name="nbsetsplayerone", type="integer") */ private $nbsetsplayerone; /** * @var int * * @orm\column(name="nbsetsplayertwo", type="integer") */ private $nbsetsplayertwo; /** * @var boolean * * @orm\column(name="isover", type="boolean") */ private $isover; public function __construct(){ $this->players = new \doctrine\common\collections\arraycollection(); }
entity player :
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; /** * player * * @orm\table(name="player") * @orm\entity(repositoryclass="appbundle\repository\playerrepository") */ class player { /** * @var int * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string * * @orm\column(name="lastname", type="string", length=255) */ private $lastname; /** * @var string * * @orm\column(name="firstname", type="string", length=255) */ private $firstname; /** * @var string * * @orm\column(name="sexe", type="string", length=255) */ private $sexe; /** * @var string * * @orm\column(name="country", type="string", length=255) */ private $country; /** * @var int * * @orm\column(name="ranking", type="integer") */ private $ranking; /** * @orm\manytomany(targetentity="appbundle\entity\game", cascade={"persist"}) */ private $games; public function __construct(){ $this->games = new \doctrine\common\collections\arraycollection(); }
app/config/config.yml :
# fosrestbundle api configuration fos_rest: format_listener: rules: - { path: '^/game', priorities: 'json', fallback_format: json, prefer_extension: true } - { path: '^/ground', priorities: 'json', fallback_format: json, prefer_extension: true } - { path: '^/player', priorities: 'json', fallback_format: json, prefer_extension: true } - { path: '^/referee', priorities: 'json', fallback_format: json, prefer_extension: true } - { path: '^/tournament-type', priorities: 'json', fallback_format: json, prefer_extension: true } - { path: '^/user', priorities: 'json', fallback_format: json, prefer_extension: true } - { path: '^/', priorities: 'json', fallback_format: json, prefer_extension: true }
gamecontroller :
/** * getgamesaction * * @get("/games") * * @param request $request * @return type */ public function getgamesaction(request $request) { $games = $this->getdoctrine()->getmanager()->getrepository("appbundle:game")->findall(); if (count($games) === 0) { $view = $this->view("no game found.", 404); return $this->handleview(); } $encoder = new jsonencoder(); $normalizer = new objectnormalizer(); $normalizer->setignoredattributes(array('games', 'players')); $normalizer->setcircularreferencehandler(function ($object) { return $object->getid(); }); $serializer = new serializer(array($normalizer), array($encoder)); $data = $serializer->serialize($games, 'json'); $view = $this->view($data) ->settemplatevar('games') ->settemplate($this->template); // ->setheader('content-type', 'application/json; charset=utf-8'); return $this->handleview($view); }
here headers used /games :
cache-control: no-cache connection: close content-type: application/json date: tue, 12 sep 2017 12:35:45 +0200, tue, 12 sep 2017 10:35:45 gmt host: localhost:8000 x-debug-token: d072dd x-debug-token-link: http://localhost:8000/app_dev.php/_profiler/d072dd x-powered-by: php/7.1.1 accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-encoding: gzip, deflate accept-language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 cache-control: max-age=0 connection: keep-alive cookie: cookieconsent_dismissed=yes; _ga=ga1.1.372052120.1482146348; phpsessid=l1fgb45492lr02a6hi10rat7ha host: localhost:8000 upgrade-insecure-requests: 1 user-agent: mozilla/5.0 (windows nt 10.0; wow64; rv:55.0) gecko/20100101 firefox/55.0
and /players :
cache-control: no-cache connection: close content-type: application/json date: tue, 12 sep 2017 12:38:18 +0200, tue, 12 sep 2017 10:38:18 gmt host: localhost:8000 x-debug-token: 609af7 x-debug-token-link: http://localhost:8000/app_dev.php/_profiler/609af7 x-powered-by: php/7.1.1 accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-encoding: gzip, deflate accept-language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 connection: keep-alive cookie: cookieconsent_dismissed=yes; _ga=ga1.1.372052120.1482146348; phpsessid=l1fgb45492lr02a6hi10rat7ha host: localhost:8000 upgrade-insecure-requests: 1 user-agent: mozilla/5.0 (windows nt 10.0; wow64; rv:55.0) gecko/20100101 firefox/55.0
you can access example of /games rendered json view : json shared url
application/json content type not taken correclty, manage work around in countroller fixing manually utf-8 encoding (which default encoding of application/json) , string returned json object.
encoding :
$encoder = new jsonencoder(new jsonencode(json_unescaped_unicode), new jsondecode(false));
convert string in json object :
$this->view(json_decode($data, true))
my controller method :
/** * getgamesaction * * @get("/games") * * @param request $request * @return type */ public function getgamesaction(request $request) { $games = $this->getdoctrine()->getmanager()->getrepository("appbundle:game")->findall(); if (count($games) === 0) { $view = $this->view("no game found.", 404); return $this->handleview(); } $encoder = new jsonencoder(new jsonencode(json_unescaped_unicode), new jsondecode(false)); $normalizer = new objectnormalizer(); $normalizer->setignoredattributes(array('games', 'players')); $normalizer->setcircularreferencehandler(function ($object) { return $object->getid(); }); $serializer = new serializer(array($normalizer), array($encoder)); $data = $serializer->serialize($games, 'json'); $view = $this->view(json_decode($data, true)) ->settemplatevar('games') ->settemplate($this->template) ->setheader('content-type', 'application/json; charset=utf-8') ->setformat('json'); return $this->handleview($view); }
Comments
Post a Comment