|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * It's free open-source software released under the MIT License. |
| 5 | + * |
| 6 | + * @author Anatoly Nekhay <[email protected]> |
| 7 | + * @copyright Copyright (c) 2025, Anatoly Nekhay |
| 8 | + * @license https://github.com/sunrise-php/coder/blob/master/LICENSE |
| 9 | + * @link https://github.com/sunrise-php/coder |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sunrise\Coder\Codec; |
| 15 | + |
| 16 | +use JsonException; |
| 17 | +use Sunrise\Coder\CodecInterface; |
| 18 | +use Sunrise\Coder\Dictionary\MediaType; |
| 19 | +use Sunrise\Coder\Exception\CodecException; |
| 20 | + |
| 21 | +use function json_decode; |
| 22 | +use function json_encode; |
| 23 | + |
| 24 | +use const JSON_BIGINT_AS_STRING; |
| 25 | +use const JSON_OBJECT_AS_ARRAY; |
| 26 | +use const JSON_THROW_ON_ERROR; |
| 27 | + |
| 28 | +final class JsonCodec implements CodecInterface |
| 29 | +{ |
| 30 | + public const CONTEXT_KEY_DECODING_FLAGS = 'decoding_flags'; |
| 31 | + public const CONTEXT_KEY_DECODING_MAX_DEPTH = 'decoding_max_depth'; |
| 32 | + public const CONTEXT_KEY_ENCODING_FLAGS = 'encoding_flags'; |
| 33 | + public const CONTEXT_KEY_ENCODING_MAX_DEPTH = 'encoding_max_depth'; |
| 34 | + |
| 35 | + private const DEFAULT_CODING_MAX_DEPTH = 512; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param array<array-key, mixed> $context |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + private readonly array $context = [], |
| 42 | + ) { |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritDoc |
| 47 | + */ |
| 48 | + public function getSupportedMediaTypes(): array |
| 49 | + { |
| 50 | + return [MediaType::JSON]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritDoc |
| 55 | + */ |
| 56 | + public function decode(string $data, array $context = []): mixed |
| 57 | + { |
| 58 | + $context += $this->context; |
| 59 | + |
| 60 | + /** @var int $decodingFlags */ |
| 61 | + $decodingFlags = $context[self::CONTEXT_KEY_DECODING_FLAGS] ?? 0; |
| 62 | + /** @var int<1, 2147483647> $decodingMaxDepth */ |
| 63 | + $decodingMaxDepth = $context[self::CONTEXT_KEY_DECODING_MAX_DEPTH] ?? self::DEFAULT_CODING_MAX_DEPTH; |
| 64 | + |
| 65 | + $decodingFlags |= JSON_OBJECT_AS_ARRAY | JSON_BIGINT_AS_STRING; |
| 66 | + |
| 67 | + try { |
| 68 | + return json_decode($data, depth: $decodingMaxDepth, flags: $decodingFlags | JSON_THROW_ON_ERROR); |
| 69 | + } catch (JsonException $e) { |
| 70 | + throw new CodecException($e->getMessage(), previous: $e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @inheritDoc |
| 76 | + */ |
| 77 | + public function encode(mixed $data, array $context = []): string |
| 78 | + { |
| 79 | + $context += $this->context; |
| 80 | + |
| 81 | + /** @var int $encodingFlags */ |
| 82 | + $encodingFlags = $context[self::CONTEXT_KEY_ENCODING_FLAGS] ?? 0; |
| 83 | + /** @var int<1, 2147483647> $encodingMaxDepth */ |
| 84 | + $encodingMaxDepth = $context[self::CONTEXT_KEY_ENCODING_MAX_DEPTH] ?? self::DEFAULT_CODING_MAX_DEPTH; |
| 85 | + |
| 86 | + try { |
| 87 | + /** @var non-empty-string */ |
| 88 | + return json_encode($data, flags: $encodingFlags | JSON_THROW_ON_ERROR, depth: $encodingMaxDepth); |
| 89 | + } catch (JsonException $e) { |
| 90 | + throw new CodecException($e->getMessage(), previous: $e); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments