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

Jsonrecast Laravel Package

boundwize/jsonrecast

JsonRecast is an editable JSON AST for PHP with visitor traversal and formatting-preserving printing. Update JSON content programmatically while keeping indentation, spacing, and key order intact, so output differs only where you made changes—ideal for composer.json and config files.

View on GitHub
Deep Wiki
Context7

JsonRecast

Latest Version ci build Documentation Code Coverage PHPStan Downloads

Windows macOS Linux

Edit JSON content without noisy diffs

  • Update JSON content programmatically and keep the original indentation, spacing, and key order intact.
  • Transform documents with visitors, inspired by nikic/PHP-Parser, with path awareness built in.
  • The printed output differs from the input only where you made changes; nothing else is reformatted.

Installation

composer require boundwize/jsonrecast

Example

Say we want to bump a dependency, add a new one, and remove an entry in composer.json. With JsonRecast, a single visitor does all three:

<?php

use Boundwize\JsonRecast\JsonRecast;
use Boundwize\JsonRecast\Node\NodeJson;
use Boundwize\JsonRecast\Node\ObjectItemNode;
use Boundwize\JsonRecast\Node\ObjectNode;
use Boundwize\JsonRecast\Node\StringNode;
use Boundwize\JsonRecast\NodePath\NodeJsonPath;
use Boundwize\JsonRecast\NodeVisitor\NodeJsonVisitor;
use Boundwize\JsonRecast\NodeVisitor\NodeJsonVisitorAbstract;

$document = JsonRecast::parse(file_get_contents('composer.json'));

$result = JsonRecast::traverse($document, new class extends NodeJsonVisitorAbstract {
    public function enterNode(NodeJson $node, NodeJsonPath $path): null|NodeJson|int
    {
        // ~ bump monolog to ^3.6
        if ($node instanceof ObjectItemNode && $path->matches(['require']) && $node->key->value === 'monolog/monolog') {
            $node->value = new StringNode('^3.6');

            return $node;
        }

        // + add guzzle under "require"
        if ($node instanceof ObjectNode && $path->matches(['require'])) {
            $node->set('guzzlehttp/guzzle', new StringNode('^7.8'));

            return $node;
        }

        // − drop the root "minimum-stability" entry
        if ($node instanceof ObjectItemNode && $path->isRoot() && $node->key->value === 'minimum-stability') {
            return NodeJsonVisitor::REMOVE_NODE;
        }

        return null;
    }
});

// update the file with original formatting preserved
file_put_contents('composer.json', JsonRecast::print($result));

Anything the visitor didn't touch keeps its original indentation, key order, and alignment.

Documentation

Documentation is available online, with source files kept in this repository:

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.
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums
rlacerda83/query-parser
socialdept/atp-orm
cboxdk/laravel-assistant-client
socialdept/atp-resolver
socialdept/tenant-domains
tautvydasr/blade-class-names
socialdept/color-scales
maherelgamil/controller
coleus/widgets
coleus/table
coleus/users
coleus/music
coleus/health
coleus/calendar
coleus/notes
coleus/support
coleus/settings