Simple Use#
Encode#
\Arokettu\Json5\Json5Encoder::encode(mixed $value, Options $options = default)
The method aims to be compatible with a simple json_encode($value) dump.
Important shared features:
JsonSerializablesupport.Float precision depends on
serialize_precisionini config.
The main differences:
NAN,INF,-INFfloat values are supported.\Arokettu\Json5\Values\Json5Serializableinterface that takes precedence in case you need different behavior for JSON and JSON5.No generic object serialization is supported. An object must be an instance of
stdClassorArrayObject, or implementJsonSerializableorJson5Serializable. You can replicate thejson_encode()behavior by wrapping an object withArrayObject.The document is always pretty-printed.
Trailing commas are always used.
Use Options and Helper Objects to customize your output.
How to prettify JSON/JSON5#
For JSON5 you also need a parser, I will use colinodell/json5.
For JSON use a built-in json_decode.
Just parse and dump it:
<?php
// adjust to your layout
require __DIR__ . '/vendor/autoload.php';
echo \Arokettu\Json5\Json5Encoder::encode(
json5_decode(
file_get_contents("php://stdin")
)
);
Run the script:
php prettify.php < composer.json > composer.json5