Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[Serializer][JsonEncoder] Exposed json_encode and json_decode params #4283

Merged
merged 1 commit into from

5 participants

@scoolen

In JsonEncoder::decode() you are unable to change the $assoc parameter from json_decode. I created two sub-classes that handle JSON encoding and decoding while exposing all the available parameters from json_encode and json_decode. You can now do this:

$jsonEncoder = new JsonEncoder(new JsonEncode(JSON_HEX_TAG), new JsonDecode(true, 1024));
$serializer = new Serializer(array(), array($jsonEncoder));

Additionally I made json_last_error() available from JsonEncoder:

$jsonEncoder->getLastEncodingError();
$jsonEncoder->getLastDecodingError();

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: N/A
Todo: -
License of the code: MIT
Documentation PR: -

@travisbot

This pull request passes (merged 38cbbe7 into 46ffbd5).

@Baachi Baachi commented on the diff
src/Symfony/Component/Serializer/Encoder/JsonDecode.php
((11 lines not shown))
+
+namespace Symfony\Component\Serializer\Encoder;
+
+/**
+ * Decodes JSON data
+ *
+ * @author Sander Coolen <sander@jibber.nl>
+ */
+class JsonDecode implements DecoderInterface
+{
+ /**
+ * @var bool
+ */
+ private $associative = false;
+ /**
+ * @var int
@Baachi
Baachi added a note

That should be integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Baachi Baachi commented on the diff
src/Symfony/Component/Serializer/Encoder/JsonDecode.php
((10 lines not shown))
+ */
+
+namespace Symfony\Component\Serializer\Encoder;
+
+/**
+ * Decodes JSON data
+ *
+ * @author Sander Coolen <sander@jibber.nl>
+ */
+class JsonDecode implements DecoderInterface
+{
+ /**
+ * @var bool
+ */
+ private $associative = false;
+ /**
@Baachi
Baachi added a note

Missing empty line between docblock and variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Baachi Baachi commented on the diff
src/Symfony/Component/Serializer/Encoder/JsonDecode.php
((28 lines not shown))
+ private $recursionDepth = 512;
+ /**
+ * @var int
+ */
+ private $lastError = JSON_ERROR_NONE;
+
+ public function __construct($assoc = false, $depth = 512)
+ {
+ $this->associative = $assoc;
+ $this->recursionDepth = $depth;
+ }
+
+ /**
+ * Returns the last decoding error (if any)
+ *
+ * @return int
@Baachi
Baachi added a note

That should be integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fabpot
Owner
@Seldaek

This looks fine to me, I asked him to submit the PR, but I wanted to get feedback from others.

@fabpot fabpot referenced this pull request from a commit
@fabpot fabpot merged branch scoolen/json-decode-params (PR #4283)
Commits
-------

38cbbe7 Moved JSON encoding and decoding to separate classes which expose all their available parameters

Discussion
----------

[Serializer][JsonEncoder] Exposed json_encode and json_decode params

In `JsonEncoder::decode()` you are unable to change the `$assoc` parameter from `json_decode`. I created two sub-classes that handle JSON encoding and decoding while exposing all the available parameters from `json_encode` and `json_decode`. You can now do this:

```php
$jsonEncoder = new JsonEncoder(new JsonEncode(JSON_HEX_TAG), new JsonDecode(true, 1024));
$serializer = new Serializer(array(), array($jsonEncoder));
```

Additionally I made `json_last_error()` available from `JsonEncoder`:
```php
$jsonEncoder->getLastEncodingError();
$jsonEncoder->getLastDecodingError();
```

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: N/A
Todo: -
License of the code: MIT
Documentation PR: -

---------------------------------------------------------------------------

by travisbot at 2012-05-14T18:46:16Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1329496) (merged 38cbbe7 into 46ffbd5).

---------------------------------------------------------------------------

by fabpot at 2012-05-15T05:07:04Z

ping @Seldaek / @lsmith77

---------------------------------------------------------------------------

by Seldaek at 2012-05-15T09:47:48Z

This looks fine to me, I asked him to submit the PR, but I wanted to get feedback from others.
00108fb
@fabpot fabpot merged commit 38cbbe7 into symfony:master
@mmucklo mmucklo referenced this pull request from a commit
@fabpot fabpot merged branch scoolen/json-decode-params (PR #4283)
Commits
-------

38cbbe7 Moved JSON encoding and decoding to separate classes which expose all their available parameters

Discussion
----------

[Serializer][JsonEncoder] Exposed json_encode and json_decode params

In `JsonEncoder::decode()` you are unable to change the `$assoc` parameter from `json_decode`. I created two sub-classes that handle JSON encoding and decoding while exposing all the available parameters from `json_encode` and `json_decode`. You can now do this:

```php
$jsonEncoder = new JsonEncoder(new JsonEncode(JSON_HEX_TAG), new JsonDecode(true, 1024));
$serializer = new Serializer(array(), array($jsonEncoder));
```

Additionally I made `json_last_error()` available from `JsonEncoder`:
```php
$jsonEncoder->getLastEncodingError();
$jsonEncoder->getLastDecodingError();
```

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: N/A
Todo: -
License of the code: MIT
Documentation PR: -

---------------------------------------------------------------------------

by travisbot at 2012-05-14T18:46:16Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1329496) (merged 38cbbe7 into 46ffbd5).

---------------------------------------------------------------------------

by fabpot at 2012-05-15T05:07:04Z

ping @Seldaek / @lsmith77

---------------------------------------------------------------------------

by Seldaek at 2012-05-15T09:47:48Z

This looks fine to me, I asked him to submit the PR, but I wanted to get feedback from others.
2c8bfdc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 14, 2012
  1. @scoolen

    Moved JSON encoding and decoding to separate classes which expose all…

    scoolen authored
    … their available parameters
This page is out of date. Refresh to see the latest.
View
72 src/Symfony/Component/Serializer/Encoder/JsonDecode.php
@@ -0,0 +1,72 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Serializer\Encoder;
+
+/**
+ * Decodes JSON data
+ *
+ * @author Sander Coolen <sander@jibber.nl>
+ */
+class JsonDecode implements DecoderInterface
+{
+ /**
+ * @var bool
+ */
+ private $associative = false;
+ /**
@Baachi
Baachi added a note

Missing empty line between docblock and variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * @var int
@Baachi
Baachi added a note

That should be integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ */
+ private $recursionDepth = 512;
+ /**
+ * @var int
+ */
+ private $lastError = JSON_ERROR_NONE;
+
+ public function __construct($assoc = false, $depth = 512)
+ {
+ $this->associative = $assoc;
+ $this->recursionDepth = $depth;
+ }
+
+ /**
+ * Returns the last decoding error (if any)
+ *
+ * @return int
@Baachi
Baachi added a note

That should be integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * @see http://php.net/manual/en/function.json-last-error.php json_last_error
+ */
+ public function getLastError()
+ {
+ return $this->lastError;
+ }
+
+ /**
+ * Decodes a JSON string into PHP data
+ *
+ * @param string $data JSON
+ * @return mixed
+ */
+ public function decode($data, $format)
+ {
+ $decodedData = json_decode($data, $this->associative, $this->recursionDepth);
+ $this->lastError = json_last_error();
+
+ return $decodedData;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function supportsDecoding($format)
+ {
+ return JsonEncoder::FORMAT === $format;
+ }
+}
View
67 src/Symfony/Component/Serializer/Encoder/JsonEncode.php
@@ -0,0 +1,67 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Serializer\Encoder;
+
+/**
+ * Encodes JSON data
+ *
+ * @author Sander Coolen <sander@jibber.nl>
+ */
+class JsonEncode implements EncoderInterface
+{
+ /**
+ * @var int
+ */
+ private $options = 0;
+ /**
+ * @var int
+ */
+ private $lastError = JSON_ERROR_NONE;
+
+ public function __construct($bitmask = 0)
+ {
+ $this->options = $bitmask;
+ }
+
+ /**
+ * Returns the last encoding error (if any)
+ *
+ * @return int
+ * @see http://php.net/manual/en/function.json-last-error.php json_last_error
+ */
+ public function getLastError()
+ {
+ return $this->lastError;
+ }
+
+ /**
+ * Encodes PHP data to a JSON string
+ *
+ * @param mixed $data
+ * @return string
+ */
+ public function encode($data, $format)
+ {
+ $encodedJson = json_encode($data, $this->options);
+ $this->lastError = json_last_error();
+
+ return $encodedJson;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function supportsEncoding($format)
+ {
+ return JsonEncoder::FORMAT === $format;
+ }
+}
View
63 src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
@@ -18,12 +18,55 @@
*/
class JsonEncoder implements EncoderInterface, DecoderInterface
{
+ const FORMAT = 'json';
+
+ /**
+ * @var JsonEncode
+ */
+ protected $encodingImpl;
+ /**
+ * @var JsonDecode
+ */
+ protected $decodingImpl;
+
+ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
+ {
+ if (null === $encodingImpl) {
+ $encodingImpl = new JsonEncode;
+ }
+ $this->encodingImpl = $encodingImpl;
+ if (null === $decodingImpl) {
+ $decodingImpl = new JsonDecode(true);
+ }
+ $this->decodingImpl = $decodingImpl;
+ }
+
+ /**
+ * Returns the last encoding error (if any)
+ *
+ * @return int
+ */
+ public function getLastEncodingError()
+ {
+ return $this->encodingImpl->getLastError();
+ }
+
+ /**
+ * Returns the last decoding error (if any)
+ *
+ * @return int
+ */
+ public function getLastDecodingError()
+ {
+ return $this->decodingImpl->getLastError();
+ }
+
/**
* {@inheritdoc}
*/
public function encode($data, $format)
{
- return json_encode($data);
+ return $this->encodingImpl->encode($data, self::FORMAT);
}
/**
@@ -31,28 +74,22 @@ public function encode($data, $format)
*/
public function decode($data, $format)
{
- return json_decode($data, true);
+ return $this->decodingImpl->decode($data, self::FORMAT);
}
/**
- * Checks whether the serializer can encode to given format
- *
- * @param string $format format name
- * @return Boolean
+ * {@inheritdoc}
*/
public function supportsEncoding($format)
{
- return 'json' === $format;
+ return self::FORMAT === $format;
}
/**
- * Checks whether the serializer can decode from given format
- *
- * @param string $format format name
- * @return Boolean
+ * {@inheritdoc}
*/
public function supportsDecoding($format)
{
- return 'json' === $format;
+ return self::FORMAT === $format;
}
-}
+}
Something went wrong with that request. Please try again.