diff --git a/src/JsonObjectBuilder.php b/src/JsonObjectBuilder.php index f845b4d..e31bd2d 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) { @@ -39,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 b323cdb..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 = <<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); + } }