Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Json Printer Laravel Package

ergebnis/json-printer

A PHP package that provides a JSON printer for flexible indentation. It normalizes existing JSON to spaces or tabs without escaping or unescaping, making it easy to reformat JSON consistently.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package via Composer: composer require ergebnis/json-printer. The core class is Ergebnis\Json\Printer\Printer. Start with the simplest use case: taking a minified JSON string and normalizing its indentation — for example, converting single-line JSON into human-readable format with 2-space indentation:

$printer = new Printer\Printer();
$printed = $printer->print($json, '  ');

This is ideal for debugging API responses, log output, or pre-commit formatting of configuration files where json_encode(..., JSON_PRETTY_PRINT) isn’t flexible enough (e.g., switching from 4 spaces to tabs).

Implementation Patterns

  • CLI tools & scaffolding: Use Printer in console commands to format JSON fixtures or generated config files (e.g., config/extra.json) with team-specific indentation (spaces vs. tabs).
  • Test utilities: In unit/integration tests, normalize baseline JSON fixtures before comparing outputs to avoid false failures due to whitespace differences.
  • Hybrid workflows: Combine with json_encode() for controlled formatting — encode data as JSON, then re-print with custom indentation to match style guides (e.g., enforcing 2-space indents across both PHP config and JSON schema files).
  • Custom formatters: Wrap the printer in a reusable service (e.g., JsonFormatterService) that injects indentation preference from config, supporting environment-specific formatting (e.g., compact for prod logs, pretty for dev).

Gotchas and Tips

  • Only indents, no parsing: The printer assumes the input is valid JSON — it does not validate or decode. Pass invalid JSON (e.g., trailing commas) will throw JsonException. Always validate first with json_validate() (PHP ≥8.3) or json_decode(..., true, 512, JSON_THROW_ON_ERROR).
  • Indent robustness: Version ≥2.0.1 rejects mixed tabs/spaces as indent (e.g., '\t '), but allows tab-only or space-only (e.g., ' ' or "\t"). Use ' ' for 4 spaces, "\t" for tabs.
  • Idempotency: The printer is idempotent — applying it twice yields the same result. Safe to use unconditionally in pipelines.
  • Line endings: Since v2.0.0, use the optional third argument to customize newlines (e.g., Printer::print($json, ' ', "\n")), especially for cross-platform compatibility or legacy system expectations (e.g., \r\n).
  • Avoid over-engineering: This isn’t a full formatter — it preserves key formatting choices like space after colons or commas. Stick to it only for indentation consistency; for full formatting control, use json_encode($data, JSON_PRETTY_PRINT) instead.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core
nodisrupt/laravel-agent
sierratecnologia/media-manager