JSON5 Tools

Presenting a couple of JSON5 tools I built.

The first one was done a year ago, a docker CLI image to convert JSON5 to JSON: arokettu/json5-to-json.

$ echo "{ hello: 'world' }" | docker run -i --rm arokettu/json5-to-json
{"hello":"world"}

A simple tool to convert your configs on the build phase without installing anything into your images.

The second one is a builder/serializer that is aimed at creating pretty configs in PHP: arokettu/json5-builder.

While it can just serialize some data into JSON5, the goal is to create nice configs with comments and custom formatting.

<?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);
// 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 ->",
}

Comments

Comments powered by Disqus