Simple Use

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:

The main differences:

  • NAN, INF, -INF float values are supported.

  • \Arokettu\Json5\Values\Json5Serializable interface 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 stdClass or ArrayObject, or implement JsonSerializable or Json5Serializable. You can replicate the json_encode() behavior by wrapping an object with ArrayObject.

  • The document is always pretty-printed.

  • Trailing commas are always used.

Use Options and Helper Objects to customize your output.

How to prettify JSON#

You also need a parser, I will use colinodell/json5. 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