ergebnis / json-printer
Provides a JSON printer, allowing for flexible indentation.
Installs: 13 013 890
Dependents: 13
Suggesters: 0
Security: 0
Stars: 80
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: ^8.0
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- ergebnis/license: ^2.0.0
- ergebnis/php-cs-fixer-config: ^4.11.0
- fakerphp/faker: ^1.20.0
- infection/infection: ~0.26.6
- phpunit/phpunit: ^9.5.26
- psalm/plugin-phpunit: ~0.18.3
- vimeo/psalm: ^4.30.0
README
Provides a JSON printer, allowing for flexible indentation.
Installation
Run
composer require ergebnis/json-printer
Usage
Let's assume we have a variable $json
which contains some JSON that is not indented:
{"name":"Andreas Möller","emoji":"🤓","urls":["https://localheinz.com","https://github.com/localheinz","https://twitter.com/localheinz"]}
or indented with 4 spaces:
{ "name":"Andreas Möller", "emoji":"🤓", "urls":[ "https://localheinz.com", "https://github.com/localheinz", "https://twitter.com/localheinz" ] }
but we want to indent it with 2 spaces (or tabs).
This is where Ergebnis\Json\Printer\Printer
comes in
<?php declare(strict_types=1); use Ergebnis\Json\Printer; $printer = new Printer\Printer(); $printed = $printer->print( $json, ' ', );
which results in $printed
:
{ "name":"Andreas Möller", "emoji":"🤓", "urls":[ "https://localheinz.com", "https://github.com/localheinz", "https://twitter.com/localheinz" ] }
💡 Note that this printer is only concerned with normalizing the indentation, no escaping or un-escaping occurs.
Changelog
Please have a look at CHANGELOG.md
.
Contributing
Please have a look at CONTRIBUTING.md
.
Code of Conduct
Please have a look at CODE_OF_CONDUCT.md
.
Security Policy
Please have a look at SECURITY.md
.
License
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.
Credits
The Printer
is adopted from Composer\Json\JsonFormatter
(originally licensed under MIT by Nils Adermann and Jordi Boggiano), who adopted it from a blog post by Dave Perrett (originally licensed under MIT by Dave Perrett).
The PrinterTest
is inspired by Composer\Test\Json\JsonFormatterTest
(originally licensed under MIT by Nils Adermann and Jordi Boggiano), as well as ZendTest\Json\JsonTest
(originally licensed under New BSD License).
Curious what I am up to?
Follow me on Twitter!