Skip to content

Commit

Permalink
Merge pull request #3 from ergebnis/fix/object
Browse files Browse the repository at this point in the history
Fix: Always decode JSON to `object`
  • Loading branch information
localheinz authored Dec 10, 2022
2 parents b3e8d07 + 8be5d6f commit 7c0a1e3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -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
2 changes: 1 addition & 1 deletion infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"text": ".build/infection/infection-log.txt"
},
"minCoveredMsi": 100,
"minMsi": 70,
"minMsi": 66,
"phpUnit": {
"configDir": "test\/Unit"
},
Expand Down
6 changes: 6 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
<code>\Generator&lt;string, array{0: string}&gt;</code>
</MoreSpecificReturnType>
</file>
<file src="test/Unit/JsonTest.php">
<MixedAssignment occurrences="2">
<code>$decoded</code>
<code>$decoded</code>
</MixedAssignment>
</file>
</files>
2 changes: 1 addition & 1 deletion src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function fromFile(string $file): self
try {
$decoded = \json_decode(
$encoded,
true,
false,
512,
\JSON_THROW_ON_ERROR,
);
Expand Down
20 changes: 19 additions & 1 deletion test/Unit/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 7c0a1e3

Please sign in to comment.