Skip to content

[9.x] Adds Str::isJson#42545

Merged
taylorotwell merged 4 commits into
laravel:9.xfrom
ecrmnn:Str__isJson
May 30, 2022
Merged

[9.x] Adds Str::isJson#42545
taylorotwell merged 4 commits into
laravel:9.xfrom
ecrmnn:Str__isJson

Conversation

@ecrmnn

@ecrmnn ecrmnn commented May 27, 2022

Copy link
Copy Markdown
Contributor

Adds isJson() to Str and Stringable.

Str::isJson($data); 
//=> boolean

Str::of($data)->isJson()
//=> boolean

str($data)->isJson();
//=> boolean

Uses JSON_THROW_ON_ERROR flag introduced in PHP 7.3 under the hood.

@ecrmnn ecrmnn marked this pull request as ready for review May 27, 2022 14:50
@ecrmnn

ecrmnn commented Jun 4, 2022

Copy link
Copy Markdown
Contributor Author

@florisbosch

By it self '' isn't valid JSON.

json_decode('', false, 512, JSON_THROW_ON_ERROR);

// [THROWS] JsonException with message 'Syntax error'

null isn't valid because this is a string helper, therefore null will never be valid because it's not a string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants