From 5aa53a97db14d352f10c225713e4318b3eabbdd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 10 Dec 2022 23:29:38 +0100 Subject: [PATCH 1/2] Enhancement: Assert that JSON is consistently decoded to an object --- psalm-baseline.xml | 6 ++++++ test/Unit/JsonTest.php | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index f06648e..914c7f6 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -16,4 +16,10 @@ \Generator<string, array{0: string}> + + + $decoded + $decoded + + diff --git a/test/Unit/JsonTest.php b/test/Unit/JsonTest.php index 6ec9cd4..282bc6d 100644 --- a/test/Unit/JsonTest.php +++ b/test/Unit/JsonTest.php @@ -52,7 +52,15 @@ public function testFromStringReturnsJsonWhenValueIsValidJson(string $encoded): self::assertSame($encoded, $json->toString()); self::assertSame($encoded, $json->encoded()); - self::assertJsonStringEqualsJsonString($encoded, \json_encode($json->decoded())); + + $decoded = \json_decode( + $encoded, + false, + 512, + \JSON_THROW_ON_ERROR, + ); + + self::assertEquals($decoded, $json->decoded()); } public function testFromFileThrowsWhenFileDoesNotExist(): void @@ -90,5 +98,15 @@ public function testFromFileReturnsJsonWhenFileContainsValidJson(string $file): $json = Json::fromFile($file); self::assertStringEqualsFile($file, $json->toString()); + self::assertStringEqualsFile($file, $json->encoded()); + + $decoded = \json_decode( + \file_get_contents($file), + false, + 512, + \JSON_THROW_ON_ERROR, + ); + + self::assertEquals($decoded, $json->decoded()); } } From 8be5d6f1cad70349e5f4555c7a1efa2505b29478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 10 Dec 2022 23:32:57 +0100 Subject: [PATCH 2/2] Fix: Always decode JSON to object --- CHANGELOG.md | 5 +++++ infection.json | 2 +- src/Json.php | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 561119b..8144476 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), For a full diff see [`1.0.0...main`][1.0.0...main]. +### Fixed + +- Decoded JSON `string` to an object, not an associative array, when constructing `Json` from file name ([#3]), by [@localheinz] + ## [`1.0.0`][1.0.0] For a full diff see [`c020e6f...1.0.0`][c020e6f...1.0.0]. @@ -24,5 +28,6 @@ For a full diff see [`c020e6f...1.0.0`][c020e6f...1.0.0]. [#1]: https://github.com/ergebnis/json/pull/1 [#2]: https://github.com/ergebnis/json/pull/2 +[#3]: https://github.com/ergebnis/json/pull/3 [@localheinz]: https://github.com/localheinz diff --git a/infection.json b/infection.json index 4e433d1..8eddeaf 100644 --- a/infection.json +++ b/infection.json @@ -4,7 +4,7 @@ "text": ".build/infection/infection-log.txt" }, "minCoveredMsi": 100, - "minMsi": 70, + "minMsi": 66, "phpUnit": { "configDir": "test\/Unit" }, diff --git a/src/Json.php b/src/Json.php index 07b5adc..29aea6d 100644 --- a/src/Json.php +++ b/src/Json.php @@ -69,7 +69,7 @@ public static function fromFile(string $file): self try { $decoded = \json_decode( $encoded, - true, + false, 512, \JSON_THROW_ON_ERROR, );