Skip to content

rcubitto/json-pretty

Repository files navigation

JSON Pretty Print

Latest Version on Packagist Build Status Quality Score Total Downloads

JSON Pretty is a very simple library that prints a JSON array in full color with proper indentation.

Installation

You can install the package via composer:

composer require rcubitto/json-pretty

Usage

You can print any array, both sequencial and associate.

\Rcubitto\JsonPretty\JsonPretty::print([
    'store' => 'Best Buy',
    'number' => 30305,
    'products' => [
        [
            'name' => 'TV',
            'cost' => 2000.00,
            'in_stock' => true
        ],
        [
            'name' => 'Phone',
            'cost' => 350.80,
            'in_stock' => false
        ],
        [
            'name' => 'Sample',
            'cost' => 0,
            'in_stock' => null
        ]
    ]
]);

The previous snippet will return the following string:

Print output

You can also print an object class:

$obj = new \Stdclass;
$obj->prop = 1;
$obj->another = 2;

\Rcubitto\JsonPretty\JsonPretty::print($obj);

Output:

Print output

NOTE: the string is not echoed out, just returned. So if you want to actually echo it out, don't forget to do so.

Config

You can also configure different colors for different types.

$options = [
    'colors' => [
        'bracket' => 'blue',    // {}[] and object keys
        'number' => 'green',    // any is_numeric (except strings like "1")
        'string' => 'purple',
        'boolean' => 'pink',
        'null' => 'black'
    ]
];


\Rcubitto\JsonPretty\JsonPretty::print($sample, $options);

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please use the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

PHP Package Boilerplate

This package was generated using the PHP Package Boilerplate.