arokettu/json5-builder

JSON5 config builder/encoder

1.0.1 2025-04-26 21:43 UTC

This package is auto-updated.

Last update: 2025-04-27 03:39:08 UTC


README

Packagist PHP License Gitlab CI Codecov

A tool to generate human-friendly JSON5 files. The tool aims to generate new files, not modifying existing ones. However, it can prettify a raw JSON a bit.

For parsing please use other tools like colinodell/json5.

Installation

composer require arokettu/json5-builder

Usage

<?php

use Arokettu\Json5\Json5Encoder;
use Arokettu\Json5\Values\CommentDecorator;
use Arokettu\Json5\Values\HexInteger;

require __DIR__ . '/../vendor/autoload.php';

$config = new CommentDecorator(
    [
        'bareKeys' => '<- Look, no quotes!',
        'value' => new CommentDecorator(
            new HexInteger(0xFFF),
            commentAfter: 'This is a very important value'
         ),
        'notAvailableInJSON' => [NAN, INF],
        'end' => 'auto trailing comma ->'
    ],
    commentBefore: 'This is my cool JSON5 config!',
);

echo Json5Encoder::encode($config);

will result in

// This is my cool JSON5 config!
{
    bareKeys: "<- Look, no quotes!",
    value: 0xFFF, // This is a very important value
    notAvailableInJSON: [
        NaN,
        Infinity,
    ],
    end: "auto trailing comma ->",
}

Documentation

Read the full documentation here: https://sandfox.dev/php/json5-builder.html

Also on Read the Docs: https://json5-builder.readthedocs.io/

Support

Please file issues on our main repo at GitLab: https://gitlab.com/sandfox/json5-builder/-/issues

Feel free to ask any questions in our room on Gitter: https://gitter.im/arokettu/community

License

The library is available as open source under the terms of the MIT License.