Change a JSON file the way a person would: only the values that change, where they are, keeping every comment, the order of the keys and the layout of everything else.
class MMO\admin\helpers\JsonEditor
Namespace
MMO\admin\helpers
`deploy.json` is reviewed like code, and commented: its comments are the documentation of its keys a developer
has in front of them. Decoding it, changing a value and encoding it again would drop every comment and rewrite
every line -- a diff nobody can review. So the text is read into a tree that remembers where each value starts
and ends, and every change is a splice of the text at that place:
- a changed value replaces the text of the old one, and nothing else;
- a new key goes after the last key of its object, indented like it -- an object written on one line stays so;
- a removed key takes its lines with it, and the comment lines right above it, which are about it.
It reads what [`jsonDecode`](https://mcp.mmoframework.fr/documentation/mmo.core/php/strings.md#jsondecode) reads -- `//` and ` *\/` comments, trailing commas -- and a
key starting with `//` is a comment too ([`Store::stripComments`](https://mcp.mmoframework.fr/documentation/mmo.admin/php/helpers/Store.md#store-stripcomments)): [`update`](https://mcp.mmoframework.fr/documentation/mmo.admin/php/helpers/JsonEditor.md#jsoneditor-update) never removes one. After an
update the text is decoded again, by this class and by jsonDecode(), and compared with what was asked for, so a
text that mmo.admin would read differently is never handed out.
$json = new JsonEditor(\MMO\core\files\get('deploy.json'));
$json->set(['targets', 'demo', 'url'], 'https://demo.example.com');
$json->remove(['targets', 'old']);
$json->update($declaration); // or: make the whole file hold this, changing only what differs
\MMO\core\files\set('deploy.json', $json->getText());
Make the document hold $data, changing only what differs: a key it lacks is added, a key $data lacks is removed, a value that differs is replaced -- and an object present on both sides is updated k…
Put a value at $path: in place of the one there, or as a new key of its object -- created, with its parents, when it does not exist.
function set(array $path, mixed $value): staticThe new value is written whole, so what was inside the old one goes, comments included: to change a few keys
of an object and keep the rest, set those keys, or [`update`](https://mcp.mmoframework.fr/documentation/mmo.admin/php/helpers/JsonEditor.md#jsoneditor-update). Write an empty object as `new \stdClass()`:
`[]` is an empty list.
Write the object at $path one member per line, when it is written on one line -- so that each member can have a comment of its own. What is inside its members stays as it is written. An object holding a comment is left as it is: it would not survive.
Make the document hold $data, changing only what differs: a key it lacks is added, a key $data lacks is removed, a value that differs is replaced -- and an object present on both sides is updated key by key, so that what did not change inside it, comments included, stays as it is. A key starting with // is a comment, never removed.
function update(array $data): static
See
jsonDecode, which is how mmo.admin reads it. Then nothing is to be written.