Skip to content

Commit 0393267

Browse files
committed
refactor: remove unused JsonOptions and simplify Json adapter implementation
- Replaced custom logic with native JSON functions, utilizing `JSON_UNESCAPED_UNICODE` and `JSON_UNESCAPED_SLASHES`. - Removed unsupported options such as `cycle_check` and `enable_json_expr_finder`. - Add tests for serialization and deserialization Signed-off-by: mmalac <mmalac@sygic.com>
1 parent 91692e9 commit 0393267

File tree

5 files changed

+264
-125
lines changed

5 files changed

+264
-125
lines changed

psalm-baseline.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="6.14.3@d0b040a91f280f071c1abcb1b77ce3822058725a">
2+
<files psalm-version="6.15.1@28dc127af1b5aecd52314f6f645bafc10d0e11f9">
33
<file src="src/Adapter/Json.php">
44
<MoreSpecificImplementedParamType>
55
<code><![CDATA[$options]]></code>
@@ -8,12 +8,6 @@
88
<code><![CDATA[$options]]></code>
99
</NonInvariantDocblockPropertyType>
1010
</file>
11-
<file src="src/Adapter/JsonOptions.php">
12-
<PossiblyUnusedMethod>
13-
<code><![CDATA[setCycleCheck]]></code>
14-
<code><![CDATA[setEnableJsonExprFinder]]></code>
15-
</PossiblyUnusedMethod>
16-
</file>
1711
<file src="src/Adapter/PhpSerialize.php">
1812
<MoreSpecificImplementedParamType>
1913
<code><![CDATA[$options]]></code>

src/Adapter/Json.php

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
use JsonException;
99
use Laminas\Serializer\Exception;
1010

11-
use function in_array;
12-
use function is_array;
13-
use function is_int;
14-
use function is_object;
1511
use function json_decode;
1612
use function json_encode;
1713

1814
use const JSON_THROW_ON_ERROR;
15+
use const JSON_UNESCAPED_SLASHES;
16+
use const JSON_UNESCAPED_UNICODE;
1917

2018
final class Json extends AbstractAdapter
2119
{
@@ -56,15 +54,8 @@ public function getOptions(): JsonOptions
5654
*/
5755
public function serialize(mixed $value): string
5856
{
59-
$options = $this->getOptions();
60-
$cycleCheck = $options->getCycleCheck();
61-
$opts = [
62-
'enableJsonExprFinder' => $options->getEnableJsonExprFinder(),
63-
'objectDecodeType' => $options->isAssocArray(),
64-
];
65-
6657
try {
67-
return $this->encode($value, $cycleCheck, $opts);
58+
return $this->encode($value);
6859
} catch (InvalidArgumentException $e) {
6960
throw new Exception\InvalidArgumentException('Serialization failed: ' . $e->getMessage(), 0, $e);
7061
} catch (JsonException $e) {
@@ -89,45 +80,20 @@ public function unserialize(string $serialized): mixed
8980
return $ret;
9081
}
9182

92-
/**
93-
* @param mixed[] $options
94-
*/
95-
private function encode(mixed $value, bool $cycleCheck, array $options): string
83+
private function encode(mixed $value): string
9684
{
97-
if ($cycleCheck) {
98-
$seen = [];
99-
$detectCycles = function (mixed &$val) use (&$seen, &$detectCycles): void {
100-
if (is_array($val) || is_object($val)) {
101-
if (in_array($val, $seen, true)) {
102-
throw new InvalidArgumentException("Cycle detected in value to be JSON encoded");
103-
}
104-
$seen[] = $val;
105-
foreach ($val as &$item) {
106-
$detectCycles($item);
107-
}
108-
}
109-
};
110-
$detectCycles($value);
111-
}
112-
113-
$jsonOptions = isset($options['json_encode_options']) && is_int($options['json_encode_options'])
114-
? $options['json_encode_options']
115-
: 0;
116-
117-
$encoded = json_encode($value, $jsonOptions | JSON_THROW_ON_ERROR);
118-
if ($encoded === false) {
119-
throw new JsonException('Syntax error');
120-
}
121-
122-
return $encoded;
85+
return json_encode(
86+
$value,
87+
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR
88+
);
12389
}
12490

12591
private function decode(string $value, bool $assoc): mixed
12692
{
12793
return json_decode(
12894
$value,
12995
$assoc,
130-
512, // depth
96+
512,
13197
JSON_THROW_ON_ERROR
13298
);
13399
}

src/Adapter/JsonOptions.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,10 @@
44

55
namespace Laminas\Serializer\Adapter;
66

7-
use Laminas\Serializer\Exception;
8-
97
final class JsonOptions extends AdapterOptions
108
{
11-
protected bool $cycleCheck = false;
12-
13-
protected bool $enableJsonExprFinder = false;
14-
private bool $assocArray = true;
15-
16-
public function setCycleCheck(bool $flag): void
17-
{
18-
$this->cycleCheck = $flag;
19-
}
20-
21-
public function getCycleCheck(): bool
22-
{
23-
return $this->cycleCheck;
24-
}
25-
26-
public function setEnableJsonExprFinder(bool $flag): void
27-
{
28-
$this->enableJsonExprFinder = $flag;
29-
}
30-
31-
public function getEnableJsonExprFinder(): bool
32-
{
33-
return $this->enableJsonExprFinder;
34-
}
9+
private bool $assocArray = true;
3510

36-
/**
37-
* @throws Exception\InvalidArgumentException
38-
*/
3911
public function setAssocArray(bool $assocArray): void
4012
{
4113
$this->assocArray = $assocArray;

0 commit comments

Comments
 (0)