Skip to content
PHP Ecosystem
Explore

JsonFormatter

API reference › Foxy › Json › JsonFormatter

final classFoxy\Json\JsonFormatter
Sourcesrc/Json/JsonFormatter.php

Formats JSON while retaining selected empty containers as arrays.

MethodDescriptionDefined by
format() staticFormat the data in JSON.JsonFormatter
getArrayKeys() staticGet the list of keys to be retained with an array representation if they are empty.JsonFormatter
getMapKeys() staticGet the keys that were represented as empty JSON objects.JsonFormatter
ConstantValueDescriptionDefined by
ARRAY_KEYS_REGEX'/["\']([\w\-.]+)["\']\s*:\s*\[\s*]/'JsonFormatter
DEFAULT_INDENT4JsonFormatter
MAP_KEYS_REGEX'/["\']([\w\-.]+)["\']\s*:\s*\{\s*}/'JsonFormatter
public static function format(string $json, string[] $arrayKeys = [], int $indent = \self::DEFAULT_INDENT, bool $formatJson = true): string

Format the data in JSON.

Parameters

ParameterTypeDescription
$jsonstringThe original JSON.
$arrayKeysstring[]The list of keys to be retained with an array representation if they are empty.
$indentintThe space count for indent.
$formatJsonboolCheck if the JSON must be formatted.

Returns

string

Throws

public static function getArrayKeys(string $content): array

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

Parameters

ParameterTypeDescription
$contentstringThe content.

Returns

array

public static function getMapKeys(string $content): array

Get the keys that were represented as empty JSON objects.

Parameters

ParameterTypeDescription
$contentstring

Returns

array