Skip to content
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
6 changes: 4 additions & 2 deletions src/JsonObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class JsonObjectBuilder extends DataObjectBuilder {
public function __construct(
private readonly int $depth = 512,
private readonly int $flags = 0,
) {
}

Expand All @@ -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) {
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion test/phpunit/JsonObjectBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<JSON
Expand Down Expand Up @@ -215,4 +214,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);
}
}