Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Support;

use Illuminate\Support\Traits\Macroable;
use JsonException;
use League\CommonMark\GithubFlavoredMarkdownConverter;
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
use Ramsey\Uuid\Generator\CombGenerator;
Expand Down Expand Up @@ -370,6 +371,27 @@ public static function isAscii($value)
return ASCII::is_ascii((string) $value);
}

/**
* Determine if a given string is valid JSON.
*
* @param string $value
* @return bool
*/
public static function isJson($value)
{
if (! is_string($value)) {
return false;
}

try {
json_decode($value, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException) {
return false;
}

return true;
}

/**
* Determine if a given string is a valid UUID.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ public function isAscii()
return Str::isAscii($this->value);
}

/**
* Determine if a given string is valid JSON.
*
* @return bool
*/
public function isJson()
{
return Str::isJson($this->value);
}

/**
* Determine if a given string is a valid UUID.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,24 @@ public function testIsUuidWithInvalidUuid($uuid)
$this->assertFalse(Str::isUuid($uuid));
}

public function testIsJson()
{
$this->assertTrue(Str::isJson('1'));
$this->assertTrue(Str::isJson('[1,2,3]'));
$this->assertTrue(Str::isJson('[1, 2, 3]'));
$this->assertTrue(Str::isJson('{"first": "John", "last": "Doe"}'));
$this->assertTrue(Str::isJson('[{"first": "John", "last": "Doe"}, {"first": "Jane", "last": "Doe"}]'));

$this->assertFalse(Str::isJson('1,'));
$this->assertFalse(Str::isJson('[1,2,3'));
$this->assertFalse(Str::isJson('[1, 2 3]'));
$this->assertFalse(Str::isJson('{first: "John"}'));
$this->assertFalse(Str::isJson('[{first: "John"}, {first: "Jane"}]'));
$this->assertFalse(Str::isJson(''));
$this->assertFalse(Str::isJson(null));
$this->assertFalse(Str::isJson([]));
}

public function testKebab()
{
$this->assertSame('laravel-php-framework', Str::kebab('LaravelPhpFramework'));
Expand Down
17 changes: 17 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ public function testIsUuid()
$this->assertFalse($this->stringable('2cdc7039-65a6-4ac7-8e5d-d554a98')->isUuid());
}

public function testIsJson()
{
$this->assertTrue($this->stringable('1')->isJson());
$this->assertTrue($this->stringable('[1,2,3]')->isJson());
$this->assertTrue($this->stringable('[1, 2, 3]')->isJson());
$this->assertTrue($this->stringable('{"first": "John", "last": "Doe"}')->isJson());
$this->assertTrue($this->stringable('[{"first": "John", "last": "Doe"}, {"first": "Jane", "last": "Doe"}]')->isJson());

$this->assertFalse($this->stringable('1,')->isJson());
$this->assertFalse($this->stringable('[1,2,3')->isJson());
$this->assertFalse($this->stringable('[1, 2 3]')->isJson());
$this->assertFalse($this->stringable('{first: "John"}')->isJson());
$this->assertFalse($this->stringable('[{first: "John"}, {first: "Jane"}]')->isJson());
$this->assertFalse($this->stringable('')->isJson());
$this->assertFalse($this->stringable(null)->isJson());
}

public function testIsEmpty()
{
$this->assertTrue($this->stringable('')->isEmpty());
Expand Down