Skip to content

Commit

Permalink
Added new service to convert object to json
Browse files Browse the repository at this point in the history
  • Loading branch information
octante committed Apr 17, 2015
1 parent deecbd6 commit dd34c26
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Resources/config/marvel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<parameter key="octante_marvel_api.client.class">Octante\MarvelAPIBundle\Lib\Client</parameter>
<parameter key="octante_marvel_api.authentication.class">Octante\MarvelAPIBundle\Lib\Authentication</parameter>

<parameter key="octante_marvel_api.encode2json.class">Octante\MarvelAPIBundle\Services\JsonSerialize</parameter>

</parameters>

<services>
Expand Down Expand Up @@ -69,6 +71,9 @@
<argument type="service" id="marvel.client"/>
</service>

<service id="marvel.tojson" class="%octante_marvel_api.encode2json.class%">
</service>

</services>

</container>
42 changes: 42 additions & 0 deletions Services/JsonSerialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/*
* This file is part of the UrbanGarden package.
*
* (c) Issel Guberna <issel.guberna@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Octante\MarvelAPIBundle\Services;

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;


class JsonSerialize
{
/**
* @var Serializer
*/
private $serializer;

public function __construct()
{
$encoders = array(new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());

$this->serializer = new Serializer($normalizers, $encoders);
}

/**
* @param $data
*
* @return string
*/
public function encode($data)
{
return $this->serializer->serialize($data, 'json');
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"require": {
"php": ">=5.4.0",
"symfony/framework-bundle": "2.*"
"symfony/framework-bundle": "2.*",
"symfony/serializer": "3.0.*@dev"
},
"autoload": {
"psr-4": {"Octante\\MarvelAPIBundle\\": ""}
Expand Down

0 comments on commit dd34c26

Please sign in to comment.