Skip to content

Commit

Permalink
[7.x] Minor optimisations on isJson function (#619)
Browse files Browse the repository at this point in the history
Co-authored-by: Duncan McClean <duncan@duncanmcclean.com>
  • Loading branch information
indykoning and duncanmcclean authored Oct 11, 2024
1 parent c7c8798 commit 6f50d54
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Support/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ class Json
{
public static function isJson($value): bool
{
if (is_array($value) || is_object($value)) {
if (
is_array($value)
|| is_object($value)
|| (is_string($value) && ! str_starts_with($value, '[') && ! str_starts_with($value, '{'))
) {
return false;
}

// TODO: Replace this with json_validate when dropping support for PHP 8.2.
return is_array(json_decode($value, true));
}
}

0 comments on commit 6f50d54

Please sign in to comment.