|
2 | 2 |
|
3 | 3 | namespace DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType;
|
4 | 4 |
|
5 |
| -use DoclerLabs\ApiClientGenerator\Output\Copy\Schema\SerializableInterface; |
6 |
| -use Psr\Http\Message\StreamInterface; |
7 |
| - |
8 |
| -class JsonContentTypeSerializer implements ContentTypeSerializerInterface |
| 5 | +class JsonContentTypeSerializer extends AbstractJsonContentTypeSerializer |
9 | 6 | {
|
10 |
| - const MIME_TYPE = 'application/json'; |
11 |
| - const JSON_DEPTH = 512; |
12 |
| - const JSON_OPTIONS = JSON_BIGINT_AS_STRING | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_UNICODE; |
13 |
| - |
14 |
| - public function encode(SerializableInterface $body): string |
15 |
| - { |
16 |
| - $encodedData = json_encode($body->toArray(), self::JSON_OPTIONS); |
17 |
| - |
18 |
| - $lastErrorCode = json_last_error(); |
19 |
| - if ($lastErrorCode === JSON_ERROR_NONE && $encodedData !== false) { |
20 |
| - return $encodedData; |
21 |
| - } |
22 |
| - |
23 |
| - throw new SerializeException('JSON encode error: ' . $this->getErrorMessage($lastErrorCode)); |
24 |
| - } |
25 |
| - |
26 |
| - /** |
27 |
| - * @throws SerializeException |
28 |
| - */ |
29 |
| - public function decode(StreamInterface $body): array |
30 |
| - { |
31 |
| - $body->rewind(); |
32 |
| - |
33 |
| - // According to RFC7159 a JSON value MUST be an object, array, number, string, |
34 |
| - // or one of the following three literal names: false, null, true. |
35 |
| - $result = json_decode($body->getContents(), true, self::JSON_DEPTH, self::JSON_OPTIONS); |
36 |
| - |
37 |
| - $lastErrorCode = json_last_error(); |
38 |
| - if ($lastErrorCode === JSON_ERROR_NONE) { |
39 |
| - if (!is_array($result)) { |
40 |
| - $result = [ |
41 |
| - ContentTypeSerializerInterface::LITERAL_VALUE_KEY => $result, |
42 |
| - ]; |
43 |
| - } |
44 |
| - |
45 |
| - return $result; |
46 |
| - } |
47 |
| - |
48 |
| - throw new SerializeException('JSON decode error: ' . $this->getErrorMessage($lastErrorCode)); |
49 |
| - } |
| 7 | + public const MIME_TYPE = 'application/json'; |
50 | 8 |
|
51 | 9 | public function getMimeType(): string
|
52 | 10 | {
|
53 | 11 | return self::MIME_TYPE;
|
54 | 12 | }
|
55 |
| - |
56 |
| - private function getErrorMessage(int $errorCode): string |
57 |
| - { |
58 |
| - $errorMessages = [ |
59 |
| - JSON_ERROR_NONE => 'No errors', |
60 |
| - JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
61 |
| - JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
62 |
| - JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
63 |
| - JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
64 |
| - JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
65 |
| - JSON_ERROR_RECURSION => 'Recursive reference was found', |
66 |
| - JSON_ERROR_INF_OR_NAN => 'NaN or Inf was found', |
67 |
| - JSON_ERROR_UNSUPPORTED_TYPE => 'Unsupported type was found', |
68 |
| - ]; |
69 |
| - |
70 |
| - return $errorMessages[$errorCode] ?? 'Unknown error'; |
71 |
| - } |
72 | 13 | }
|
0 commit comments