-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonEncoder.php
More file actions
67 lines (58 loc) · 1.89 KB
/
JsonEncoder.php
File metadata and controls
67 lines (58 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace KaririCode\Serializer\Encoder;
use KaririCode\Serializer\Contract\Encoder;
use KaririCode\Serializer\Contract\SerializationContext;
use KaririCode\Serializer\Exception\SerializationException;
/**
* Encodes/decodes data as RFC 8259-compliant JSON.
*
* Uses JSON_THROW_ON_ERROR for deterministic error handling.
* Supports `pretty` context parameter for human-readable output.
*
* @package KaririCode\Serializer\Encoder
* @author Walmir Silva <walmir.silva@kariricode.org>
* @since 3.1.0 ARFA 1.3
*/
final readonly class JsonEncoder implements Encoder
{
/** @param array<mixed> $data */
#[\Override]
public function encode(array $data, SerializationContext $context): string
{
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR;
if ($context->getParameter('pretty', false)) {
$flags |= JSON_PRETTY_PRINT;
}
try {
return json_encode($data, $flags);
} catch (\JsonException $e) {
throw SerializationException::encodingFailed('json', $e->getMessage());
}
}
/** @return array<mixed> */
#[\Override]
public function decode(string $payload, SerializationContext $context): array
{
try {
$result = json_decode($payload, true, 512, JSON_THROW_ON_ERROR);
if (! \is_array($result)) {
throw new \JsonException('Root must be object or array.');
}
/** @var array<string, mixed> $result */
return $result;
} catch (\JsonException $e) {
throw SerializationException::decodingFailed('json', $e->getMessage());
}
}
#[\Override]
public function supports(string $format): bool
{
return $format === 'json';
}
#[\Override]
public function getFormat(): string
{
return 'json';
}
}