diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index b15ee1c6d633..2219b15d7a17 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -63,14 +63,15 @@ public function get($path, $lock = false) * Get the contents of a file as decoded JSON. * * @param string $path + * @param int $flags * @param bool $lock * @return array * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ - public function json($path, $lock = false) + public function json($path, $flags = 0, $lock = false) { - return json_decode($this->get($path, $lock), true); + return json_decode($this->get($path, $lock), true, 512, $flags); } /** diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index 88ffe14dfdfe..4a4fa1486b64 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -262,6 +262,20 @@ public function get($path) } } + /** + * Get the contents of a file as decoded JSON. + * + * @param string $path + * @param int $flags + * @return array|null + */ + public function json($path, $flags = 0) + { + $content = $this->get($path); + + return is_null($content) ? null : json_decode($content, true, 512, $flags); + } + /** * Create a streamed response for a given file. * diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 51d6d669c0a3..d6cc99c64dcd 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -184,6 +184,20 @@ public function testGetFileNotFound() $this->assertNull($filesystemAdapter->get('file.txt')); } + public function testJsonReturnsDecodedJsonData() + { + $this->filesystem->write('file.json', '{"foo": "bar"}'); + $filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter); + $this->assertSame(['foo' => 'bar'], $filesystemAdapter->json('file.json')); + } + + public function testJsonReturnsNullIfJsonDataIsInvalid() + { + $this->filesystem->write('file.json', '{"foo":'); + $filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter); + $this->assertNull($filesystemAdapter->json('file.json')); + } + public function testMimeTypeNotDetected() { $this->filesystem->write('unknown.mime-type', '');