Skip to content

Commit

Permalink
[9.x] Adds Str::isJson (laravel#42545)
Browse files Browse the repository at this point in the history
* Adds Str::isJson()

* Removes unnecessary test

* Fixes code style

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
ecrmnn and taylorotwell authored May 30, 2022
1 parent 8ff75a3 commit ffc35c5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
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

0 comments on commit ffc35c5

Please sign in to comment.