JsonFile
Helper for working with JSON files.
- Full name:
\Drupal\helper\File\JsonFile - Parent class:
\Drupal\helper\File\File
Methods
decodeData
Decode a file's contents into data.
public decodeData(string $data): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data |
string | |
| The contents of a file. |
Return Value:
The decoded file data.
Throws:
JsonException
encodeData
Encode data for file content.
public encodeData(mixed $data): string
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data |
mixed | |
| The data to save to a file. |
Return Value:
The encoded data, ready to be saved to a file.
Throws:
JsonException
Inherited methods
__construct
File constructor.
public __construct(string $filename, \Drupal\Core\Cache\CacheBackendInterface|bool $cache = true): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$filename |
string | |
| The filename/URI to the file. | ||
$cache |
\Drupal\Core\Cache\CacheBackendInterface|bool | |
| An optional cache backend or FALSE to disable file contents caching. |
getCache
Get the file cache data.
protected getCache(): mixed
Return Value:
The file data or NULL if no cached data was available.
setCache
Set the file cache data.
protected setCache(mixed $data): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data |
mixed | |
| The file data. |
invalidateCache
Invalidate the file cache data.
protected invalidateCache(): void
readData
Gets contents of a file.
public readData(): mixed
Return Value:
The decoded file contents.
Throws:
IOException
decodeData
Decode a file's contents into data.
public decodeData(string $data): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data |
string | |
| The contents of a file. |
Return Value:
The decoded file data.
encodeData
Encode data for file content.
public encodeData(mixed $data): string
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data |
mixed | |
| The data to save to a file. |
Return Value:
The encoded data, ready to be saved to a file.
writeData
Writes contents to a file.
public writeData(mixed $data): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$data |
mixed | |
| The contents to write to a file. If not a string, this will JSON encode | ||
| the data before writing to the file. |
Throws:
IOException
validateWritable
Validates that the directory and file are both writable.
public validateWritable(): void
Throws:
IOException
validateReadable
Validates that the directory is and file are readable.
public validateReadable(): void
Throws:
IOException