Skip to content

Commit

Permalink
use json_decode_with_error/json_encode_with_error internally - closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Nov 22, 2020
1 parent fc07cf2 commit ea8156f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/Nuxed/Json/decode.hack
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ namespace Nuxed\Json;
*/
function decode(string $json, bool $assoc = true): dynamic {
try {
$value = \json_decode(
$error = null;
$value = \json_decode_with_error(
$json,
inout $error,
$assoc,
512,
\JSON_BIGINT_AS_STRING |
Expand All @@ -26,9 +28,8 @@ function decode(string $json, bool $assoc = true): dynamic {
);
}

$error = \json_last_error();
if (\JSON_ERROR_NONE !== $error) {
throw new Exception\JsonDecodeException(Errors[$error], $error);
if ($error is nonnull && \JSON_ERROR_NONE !== $error[0]) {
throw new Exception\JsonDecodeException(Errors[$error[0]], $error[0]);
}

return $value;
Expand Down
8 changes: 4 additions & 4 deletions src/Nuxed/Json/encode.hack
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ function encode(mixed $value, bool $pretty = false, int $flags = 0): string {
$flags |= \JSON_PRETTY_PRINT;
}

$json = \json_encode($value, $flags);
$error = \json_last_error();
if (\JSON_ERROR_NONE !== $error) {
throw new Exception\JsonEncodeException(Errors[$error], $error);
$error = null;
$json = \json_encode_with_error($value, inout $error, $flags);
if ($error is nonnull && \JSON_ERROR_NONE !== $error[0]) {
throw new Exception\JsonEncodeException(Errors[$error[0]], $error[0]);
}

return $json;
Expand Down

0 comments on commit ea8156f

Please sign in to comment.