Skip to content

Commit a8827ea

Browse files
committed
Moved JSON encoding and decoding to separate classes which expose all their available parameters
1 parent 2787fde commit a8827ea

File tree

3 files changed

+189
-13
lines changed

3 files changed

+189
-13
lines changed

Encoder/JsonDecode.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Encoder;
13+
14+
/**
15+
* Decodes JSON data
16+
*
17+
* @author Sander Coolen <[email protected]>
18+
*/
19+
class JsonDecode implements DecoderInterface
20+
{
21+
/**
22+
* @var bool
23+
*/
24+
private $associative = false;
25+
/**
26+
* @var int
27+
*/
28+
private $recursionDepth = 512;
29+
/**
30+
* @var int
31+
*/
32+
private $lastError = JSON_ERROR_NONE;
33+
34+
public function __construct($assoc = false, $depth = 512)
35+
{
36+
$this->associative = $assoc;
37+
$this->recursionDepth = $depth;
38+
}
39+
40+
/**
41+
* Returns the last decoding error (if any)
42+
*
43+
* @return int
44+
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
45+
*/
46+
public function getLastError()
47+
{
48+
return $this->lastError;
49+
}
50+
51+
/**
52+
* Decodes a JSON string into PHP data
53+
*
54+
* @param string $data JSON
55+
* @return mixed
56+
*/
57+
public function decode($data, $format)
58+
{
59+
$decodedData = json_decode($data, $this->associative, $this->recursionDepth);
60+
$this->lastError = json_last_error();
61+
62+
return $decodedData;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function supportsDecoding($format)
69+
{
70+
return JsonEncoder::FORMAT === $format;
71+
}
72+
}

Encoder/JsonEncode.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Encoder;
13+
14+
/**
15+
* Encodes JSON data
16+
*
17+
* @author Sander Coolen <[email protected]>
18+
*/
19+
class JsonEncode implements EncoderInterface
20+
{
21+
/**
22+
* @var int
23+
*/
24+
private $options = 0;
25+
/**
26+
* @var int
27+
*/
28+
private $lastError = JSON_ERROR_NONE;
29+
30+
public function __construct($bitmask = 0)
31+
{
32+
$this->options = $bitmask;
33+
}
34+
35+
/**
36+
* Returns the last encoding error (if any)
37+
*
38+
* @return int
39+
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
40+
*/
41+
public function getLastError()
42+
{
43+
return $this->lastError;
44+
}
45+
46+
/**
47+
* Encodes PHP data to a JSON string
48+
*
49+
* @param mixed $data
50+
* @return string
51+
*/
52+
public function encode($data, $format)
53+
{
54+
$encodedJson = json_encode($data, $this->options);
55+
$this->lastError = json_last_error();
56+
57+
return $encodedJson;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function supportsEncoding($format)
64+
{
65+
return JsonEncoder::FORMAT === $format;
66+
}
67+
}

Encoder/JsonEncoder.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,78 @@
1818
*/
1919
class JsonEncoder implements EncoderInterface, DecoderInterface
2020
{
21+
const FORMAT = 'json';
22+
23+
/**
24+
* @var JsonEncode
25+
*/
26+
protected $encodingImpl;
27+
/**
28+
* @var JsonDecode
29+
*/
30+
protected $decodingImpl;
31+
32+
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
33+
{
34+
if (null === $encodingImpl) {
35+
$encodingImpl = new JsonEncode;
36+
}
37+
$this->encodingImpl = $encodingImpl;
38+
if (null === $decodingImpl) {
39+
$decodingImpl = new JsonDecode(true);
40+
}
41+
$this->decodingImpl = $decodingImpl;
42+
}
43+
44+
/**
45+
* Returns the last encoding error (if any)
46+
*
47+
* @return int
48+
*/
49+
public function getLastEncodingError()
50+
{
51+
return $this->encodingImpl->getLastError();
52+
}
53+
54+
/**
55+
* Returns the last decoding error (if any)
56+
*
57+
* @return int
58+
*/
59+
public function getLastDecodingError()
60+
{
61+
return $this->decodingImpl->getLastError();
62+
}
63+
2164
/**
2265
* {@inheritdoc}
2366
*/
2467
public function encode($data, $format)
2568
{
26-
return json_encode($data);
69+
return $this->encodingImpl->encode($data, self::FORMAT);
2770
}
2871

2972
/**
3073
* {@inheritdoc}
3174
*/
3275
public function decode($data, $format)
3376
{
34-
return json_decode($data, true);
77+
return $this->decodingImpl->decode($data, self::FORMAT);
3578
}
3679

3780
/**
38-
* Checks whether the serializer can encode to given format
39-
*
40-
* @param string $format format name
41-
* @return Boolean
81+
* {@inheritdoc}
4282
*/
4383
public function supportsEncoding($format)
4484
{
45-
return 'json' === $format;
85+
return self::FORMAT === $format;
4686
}
4787

4888
/**
49-
* Checks whether the serializer can decode from given format
50-
*
51-
* @param string $format format name
52-
* @return Boolean
89+
* {@inheritdoc}
5390
*/
5491
public function supportsDecoding($format)
5592
{
56-
return 'json' === $format;
93+
return self::FORMAT === $format;
5794
}
58-
}
95+
}

0 commit comments

Comments
 (0)