- How do I install boundwize/jsonrecast with Composer?
- Run composer require boundwize/jsonrecast in your project. The library is framework-agnostic, so no service provider or config file is required in Laravel. Wrap the parser, printer, and visitor pipeline in your own adapter class and resolve that from Laravel's container where needed. Pin the package version in composer.json and run composer audit as part of CI.
- Is JsonRecast a Laravel package?
- No. It is a framework-agnostic Composer library, not a Laravel package with a service provider or facade. That is acceptable for Laravel use: bind it behind a thin application-owned service or adapter so the rest of your codebase depends on a stable internal API. Keep third-party AST and visitor classes out of your domain code.
- How is JsonRecast different from json_decode and json_encode?
- Native json_decode and json_encode round-trips reformat the whole document, which produces noisy diffs. JsonRecast parses JSON into an editable AST and prints it back while keeping the original indentation, spacing, and key order, so the output differs only where you made changes. That formatting-preserving printer is the main differentiator. Use it for low-frequency document editing, not for request and response payloads.
- Which PHP and Laravel versions does JsonRecast support?
- The README shows CI, PHPStan max, and coverage badges, but the excerpt does not state an explicit PHP or Laravel support matrix. Check the Packagist page, the require constraints in composer.json, and the release notes before adopting. Run a short spike on your target PHP version. Because the library is framework-agnostic, Laravel compatibility mostly comes down to your PHP version.
- Does JsonRecast preserve key order, indentation, line endings, and trailing newlines?
- The stated goal is that printed output differs from the input only where changes were made, including indentation, spacing, and key order. The README excerpt does not explicitly confirm line ending handling, trailing newlines, Unicode escapes, or numeric precision. Build a characterization test corpus from real project JSON files covering CRLF, tabs, and Unicode, then compare the output byte for byte before relying on it.
- Does JsonRecast support JSONC or JSON5 with comments?
- The documentation excerpt does not establish whether JSONC comments or JSON5 syntax are supported, so assume strict JSON until you verify otherwise. If your files contain comments, for example some config or tsconfig-style files, test parsing explicitly during your spike. Where comments are required, use a parser that documents that support rather than assuming it.
- What are good use cases for JsonRecast in a Laravel application?
- Good fits are controlled, low-frequency document edits such as patching composer.json dependencies, updating config-as-code files, generating test fixtures, and applying repeatable API contract changes. It works well inside Artisan commands, queued jobs, code-generation services, and admin-side config patching workflows. Avoid it for high-volume request and response serialization, where native JSON functions are simpler and faster.
- How do I validate JsonRecast output before writing it to disk?
- Always parse the printed output with native json_decode and, where relevant, validate it against a JSON Schema before writing. Do this in tests and add a runtime guard inside your adapter. Also assert that unchanged regions remain byte-identical, and write atomically via a temp file plus rename so a bad mutation cannot corrupt a config file.
- How does JsonRecast report parse errors, duplicate keys, and invalid mutations?
- The excerpt does not specify the exception types or how duplicate keys, BOMs, and invalid visitor mutations are handled. Treat this as unknown and probe it in a spike using malformed JSON, duplicate keys, and deliberately invalid edits. Catch failures inside your adapter and translate them into your own exception types so callers never see third-party AST classes.
- Is JsonRecast safe for production, and what are the alternatives?
- The signals are positive: MIT license, CI, PHPStan max, coverage, docs, and regular releases. However, adoption is very low with roughly five stars and no dependents, so bus factor, API stability, and formatting edge cases are the main risks. Pin the version, isolate the library behind an internal adapter, and keep a fallback; if formatting preservation is not required, native json_decode and json_encode remain the lower-risk option.