Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] add Storage::json() method to read and decode a json file #46548

Merged
merged 2 commits into from
Mar 22, 2023
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
5 changes: 3 additions & 2 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a breaking change? The $lock parameter is now third instead of second?

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);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Filesystem/FilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '');
Expand Down