|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace ExceptionalJSON |
| 4 | +{ |
| 5 | + /** |
| 6 | + * Decodes a JSON string. |
| 7 | + * |
| 8 | + * @param string $json The JSON string being decoded. |
| 9 | + * @param bool $assoc When TRUE, returned objects will be converted into associative arrays. |
| 10 | + * @param int $depth User specified recursion depth. |
| 11 | + * @param int $options Bit mask of JSON decode options. |
| 12 | + * @return mixed The value encoded in JSON in appropriate PHP type. |
| 13 | + * @throws DecodeErrorException When the decode operation fails. |
| 14 | + */ |
| 15 | + function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
| 16 | + { |
| 17 | + $result = \json_decode($json, $assoc, $depth, $options); |
| 18 | + $code = \json_last_error(); |
| 19 | + |
| 20 | + if ($code !== \JSON_ERROR_NONE) { |
| 21 | + throw new DecodeErrorException($code, \json_last_error_msg(), $json); |
| 22 | + } |
| 23 | + |
| 24 | + return $result; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns the JSON representation of a value. |
| 29 | + * |
| 30 | + * @param mixed $value The value being encoded. |
| 31 | + * @param int $depth User specified recursion depth. |
| 32 | + * @param int $options Bit mask of JSON encode options. |
| 33 | + * @return string JSON encoded string. |
| 34 | + * @throws Exception When the encode operation fails |
| 35 | + */ |
| 36 | + function encode($value, int $options = 0, int $depth = 512) |
| 37 | + { |
| 38 | + $result = \json_encode($value, $options, $depth); |
| 39 | + $code = \json_last_error(); |
| 40 | + |
| 41 | + if ($code !== \JSON_ERROR_NONE) { |
| 42 | + throw new EncodeErrorException($code, \json_last_error_msg(), $value); |
| 43 | + } |
| 44 | + |
| 45 | + return $result; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +namespace |
| 50 | +{ |
| 51 | + use function ExceptionalJSON\decode; |
| 52 | + use function ExceptionalJSON\encode; |
| 53 | + |
| 54 | + if (!function_exists('json_try_decode')) { |
| 55 | + /** |
| 56 | + * Decodes a JSON string. |
| 57 | + * |
| 58 | + * @param string $json The JSON string being decoded. |
| 59 | + * @param bool $assoc When TRUE, returned objects will be converted into associative arrays. |
| 60 | + * @param int $depth User specified recursion depth. |
| 61 | + * @param int $options Bit mask of JSON decode options. |
| 62 | + * @return mixed The value encoded in JSON in appropriate PHP type. |
| 63 | + * @throws \ExceptionalJSON\DecodeErrorException When the decode operation fails. |
| 64 | + */ |
| 65 | + function json_try_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
| 66 | + { |
| 67 | + return decode($json, $assoc, $depth, $options); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (!function_exists('json_try_encode')) { |
| 72 | + /** |
| 73 | + * Returns the JSON representation of a value. |
| 74 | + * |
| 75 | + * @param mixed $value The value being encoded. |
| 76 | + * @param int $depth User specified recursion depth. |
| 77 | + * @param int $options Bit mask of JSON encode options. |
| 78 | + * @return string JSON encoded string. |
| 79 | + * @throws \ExceptionalJSON\EncodeErrorException When the encode operation fails |
| 80 | + */ |
| 81 | + function json_try_encode($value, int $options = 0, int $depth = 512) |
| 82 | + { |
| 83 | + return encode($value, $options, $depth); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments