Skip to content

Repository files navigation

JsonRecast

JsonRecast Logo

Editable JSON AST with visitor traversal and formatting-preserving printing.

Latest Versionci buildDocumentationCode CoveragePHPStanDownloads

WindowsmacOSLinux

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:

Demo of JsonRecast bumping a dependency, adding a new one, and removing an entry in composer.json while preserving the original formatting, including non-standard alignment

<?phpuseBoundwize\JsonRecast\JsonRecast;
useBoundwize\JsonRecast\Node\NodeJson;
useBoundwize\JsonRecast\Node\ObjectItemNode;
useBoundwize\JsonRecast\Node\ObjectNode;
useBoundwize\JsonRecast\Node\StringNode;
useBoundwize\JsonRecast\NodePath\NodeJsonPath;
useBoundwize\JsonRecast\NodeVisitor\NodeJsonVisitor;
useBoundwize\JsonRecast\NodeVisitor\NodeJsonVisitorAbstract;
$document = JsonRecast::parse(file_get_contents('composer.json'));
$result = JsonRecast::traverse($document, newclassextends NodeJsonVisitorAbstract {
publicfunctionenterNode(NodeJson$node, NodeJsonPath$path): null|NodeJson|int
{
// ~ bump monolog to ^3.6if ($nodeinstanceof ObjectItemNode && $path->matches(['require']) && $node->key->value === 'monolog/monolog') {
$node->value = newStringNode('^3.6');
return$node;
}
// + add guzzle under "require"if ($nodeinstanceof ObjectNode && $path->matches(['require'])) {
$node->set('guzzlehttp/guzzle', newStringNode('^7.8'));
return$node;
}
// − drop the root "minimum-stability" entryif ($nodeinstanceof ObjectItemNode && $path->isRoot() && $node->key->value === 'minimum-stability') {
return NodeJsonVisitor::REMOVE_NODE;
}
returnnull;
}
});
// update the file with original formatting preservedfile_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:

About

🌳 A PHP JSON parser that turns JSON into an editable AST, supports visitor-based traversal, and prints changes back while preserving the original formatting.

Resources

Contributing

Stars

4 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages