14/07
2018
Wallogit.com
2017 © Pedro Peláez
Optimizes JSON responses by minimizing duplication and improving compressibility.
PHP implementation of the node module graphql-crunch., (*1)
Optimizes JSON responses by minimizing duplication and improving compressibility., (*2)
composer require gromnan/json-crunch
We flatten the object hierarchy into an array using a post-order traversal of the object graph. As we traverse we efficiently check if we've come across a value before, including arrays and objects, and replace it with a reference to it's earlier occurence if we've seen it. Values are only ever present in the array once., (*3)
use function GromNaN\JsonCrunch\crunch;
$data = json_decode('{"a":["a","b","a"],"b":["a","b","a"]}');
$crunched = crunch($data);
echo json_encode($data, JSON_PRETTY_PRINT);
Result:, (*4)
[
"a",
"b",
[0,1,0],
{"a":2,"b":2}
]
bin/crunch-json <file>