boundwize / jsonrecast
Editable JSON AST with visitor traversal and formatting-preserving printing.
Fund package maintenance!
1.0.36
2026-08-15 00:35 UTC
Requires
- php: ^8.2
Requires (Dev)
- boundwize/pyrameter: ^0.5
- boundwize/structarmed: ^0.15
- laminas/laminas-coding-standard: ^3.1
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^11.5
- rector/rector: ^2.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main / 1.x-dev
- 1.0.36
- 1.0.35
- 1.0.34
- 1.0.33
- 1.0.32
- 1.0.31
- 1.0.30
- 1.0.29
- 1.0.28
- 1.0.27
- 1.0.26
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.4.19
- 0.4.18
- 0.4.17
- 0.4.16
- 0.4.15
- 0.4.14
- 0.4.13
- 0.4.12
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.44
- 0.3.43
- 0.3.42
- 0.3.41
- 0.3.40
- 0.3.39
- 0.3.38
- 0.3.37
- 0.3.36
- 0.3.35
- 0.3.34
- 0.3.33
- 0.3.32
- 0.3.31
- 0.3.30
- 0.3.29
- 0.3.28
- 0.3.27
- 0.3.26
- 0.3.25
- 0.3.24
- 0.3.23
- 0.3.22
- 0.3.21
- 0.3.20
- 0.3.19
- 0.3.18
- 0.3.17
- 0.3.16
- 0.3.15
- 0.3.14
- 0.3.13
- 0.3.12
- 0.3.11
- 0.3.10
- 0.3.9
- 0.3.8
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.24
- 0.2.23
- 0.2.22
- 0.2.21
- 0.2.20
- 0.2.19
- 0.2.18
- 0.2.17
- 0.2.16
- 0.2.15
- 0.2.14
- 0.2.13
- 0.2.12
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.3
- 0.0.2
- 0.0.1
- dev-samsonasik-patch-1
This package is auto-updated.
Last update: 2026-09-04 03:16:30 UTC
README
Editable JSON AST with visitor traversal and formatting-preserving printing.
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:
- Documentation site: https://boundwize.github.io/jsonrecast/
- Documentation source: docs/ for local edits and GitHub Pages publishing.