From 18681cc612fa2273b732d91088c979ac26ef7f81 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 15 Sep 2022 12:27:53 +0100 Subject: [PATCH 1/2] feature: pass flags to json_decode --- src/JsonObjectBuilder.php | 3 ++- test/phpunit/JsonObjectBuilderTest.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/JsonObjectBuilder.php b/src/JsonObjectBuilder.php index f845b4d..1ae7bac 100644 --- a/src/JsonObjectBuilder.php +++ b/src/JsonObjectBuilder.php @@ -15,6 +15,7 @@ class JsonObjectBuilder extends DataObjectBuilder { public function __construct( private readonly int $depth = 512, + private readonly int $flags = 0, ) { } @@ -23,7 +24,7 @@ public function fromJsonString(string $jsonString):JsonObject { $json = json_decode( $jsonString, depth: $this->depth, - flags: JSON_THROW_ON_ERROR + flags: JSON_THROW_ON_ERROR | $this->flags, ); } catch(NativeJsonException $exception) { diff --git a/test/phpunit/JsonObjectBuilderTest.php b/test/phpunit/JsonObjectBuilderTest.php index b323cdb..cca52dd 100644 --- a/test/phpunit/JsonObjectBuilderTest.php +++ b/test/phpunit/JsonObjectBuilderTest.php @@ -215,4 +215,18 @@ public function testFromJson_depth() { self::expectExceptionMessage("Error decoding JSON: Maximum stack depth exceeded"); $sut->fromJsonString($jsonString); } + + public function testFromJson_customFlag_bigInt() { + $jsonString = '{"num": 9876543210987654321 }'; + + $sut = new JsonObjectBuilder(); + $json = $sut->fromJsonString($jsonString); + $num = $json->getString("num"); + self::assertStringContainsString("E", $num); + + $sut = new JsonObjectBuilder(flags: JSON_BIGINT_AS_STRING); + $json = $sut->fromJsonString($jsonString); + $num = $json->getString("num"); + self::assertSame("9876543210987654321", $num); + } } From e79e8847d31a8afc4026ef331a81f58f33d6fe53 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 15 Sep 2022 12:30:24 +0100 Subject: [PATCH 2/2] tweak: code improvements --- src/JsonObjectBuilder.php | 3 ++- test/phpunit/JsonObjectBuilderTest.php | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JsonObjectBuilder.php b/src/JsonObjectBuilder.php index 1ae7bac..e31bd2d 100644 --- a/src/JsonObjectBuilder.php +++ b/src/JsonObjectBuilder.php @@ -40,9 +40,10 @@ public function fromJsonString(string $jsonString):JsonObject { public function fromJsonDecoded( object|array|string|int|float|bool|null $jsonDecoded ):JsonObject { + /** @noinspection PhpDeprecatedStdLibCallInspection */ if(is_array($jsonDecoded) && !is_int(key($jsonDecoded))) { -// The JSON could represent an primitive indexed array, but the json could have +// The JSON could represent a primitive indexed array, but the json could have // been decoded as an associative array too. Deal with associative arrays first. $jsonData = $this->fromJsonDecoded( (object)$jsonDecoded diff --git a/test/phpunit/JsonObjectBuilderTest.php b/test/phpunit/JsonObjectBuilderTest.php index cca52dd..4e2be09 100644 --- a/test/phpunit/JsonObjectBuilderTest.php +++ b/test/phpunit/JsonObjectBuilderTest.php @@ -11,7 +11,6 @@ use Gt\Json\JsonPrimitive\JsonNullPrimitive; use Gt\Json\JsonPrimitive\JsonStringPrimitive; use PHPUnit\Framework\TestCase; -use stdClass; class JsonObjectBuilderTest extends TestCase { private string $jsonStringSimpleKVP = <<