# JsonFile

> Preserves empty arrays and objects while reading and writing package manifests.

[API reference](/docs/api/index.md) › [Foxy](/docs/extensions/foxy/reference/index.md) › [Json](/docs/extensions/foxy/reference/json/index.md) › JsonFile

| final class | `Foxy\Json\JsonFile` |
| --- | --- |
| Extends | `Composer\Json\JsonFile` |
| Source | [`src/Json/JsonFile.php`](https://github.com/php-forge/foxy/blob/main/src/Json/JsonFile.php#L17) |

Preserves empty arrays and objects while reading and writing package manifests.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`encode()`](#method-encode) static | Encode package manifest data as JSON. | `JsonFile` |
| [`getArrayKeys()`](#method-getArrayKeys) | Get the list of keys to be retained with an array representation if they are empty. | `JsonFile` |
| [`read()`](#method-read) | Reads the JSON document and records how its empty containers are represented. | `JsonFile` |
| [`write()`](#method-write) | Writes package data while preserving empty-array keys from the original document. | `JsonFile` |

## Method details

### encode() <span id="method-encode"></span>

```php
public static function encode(mixed $data, int $options = \self::DEFAULT_OPTIONS, string $indent = \self::INDENT_DEFAULT): string
```

Encode package manifest data as JSON.

Empty root arrays are encoded as objects because package manifests must have an object root.
The `$indent` argument is retained for Composer compatibility; Foxy always uses four spaces.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `mixed` |  |
| `$options` | `int` |  |
| `$indent` | `string` |  |

**Returns**

`string`

### getArrayKeys() <span id="method-getArrayKeys"></span>

```php
public function getArrayKeys(): array
```

Get the list of keys to be retained with an array representation if they are empty.

**Returns**

`array`

### read() <span id="method-read"></span>

```php
public function read(): array
```

Reads the JSON document and records how its empty containers are represented.

**Returns**

`array`

### write() <span id="method-write"></span>

```php
public function write(array $hash, int $options = \self::DEFAULT_OPTIONS): void
```

Writes package data while preserving empty-array keys from the original document.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$hash` | `array` |  |
| `$options` | `int` |  |