|
1 |
| -# json-diff |
2 |
| -A PHP implementation for finding unordered diff between two JSON documents |
| 1 | +# JSON diff and rearrange tool for PHP |
| 2 | + |
| 3 | +A PHP implementation for finding unordered diff between two `JSON` documents. |
| 4 | + |
| 5 | +[](https://travis-ci.org/swaggest/json-diff) |
| 6 | +[](https://scrutinizer-ci.com/g/swaggest/json-diff/?branch=master) |
| 7 | +[](https://codeclimate.com/github/swaggest/json-diff) |
| 8 | +[](https://codeclimate.com/github/swaggest/json-diff/coverage) |
| 9 | + |
| 10 | +## Purpose |
| 11 | + |
| 12 | + * To simplify changes review between two `JSON` files you can use a standard `diff` tool on rearranged pretty-printed `JSON`. |
| 13 | + * To detect breaking changes by analyzing removals and changes from original `JSON`. |
| 14 | + * To keep original order of object sets (for example `swagger.json` [parameters](https://swagger.io/docs/specification/describing-parameters/) list). |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +### Library |
| 19 | + |
| 20 | +```bash |
| 21 | +git clone https://github.com/swaggest/json-diff.git |
| 22 | +``` |
| 23 | + |
| 24 | +### Composer |
| 25 | + |
| 26 | +[Install PHP Composer](https://getcomposer.org/doc/00-intro.md) |
| 27 | + |
| 28 | +```bash |
| 29 | +composer require swaggest/json-diff |
| 30 | +``` |
| 31 | + |
| 32 | +## Library usage |
| 33 | + |
| 34 | +Create `JsonDiff` object from two values (`original` and `new`). |
| 35 | + |
| 36 | +```php |
| 37 | +$r = new JsonDiff(json_decode($originalJson), json_decode($newJson)); |
| 38 | +``` |
| 39 | + |
| 40 | +On construction `JsonDiff` will build `rearranged` value of `new` recursively keeping `original` keys order where possible. |
| 41 | +Keys that are missing in `original` will be appended to the end of `rearranged` value in same order they had in `new` value. |
| 42 | + |
| 43 | +If two values are arrays of objects, `JsonDiff` will try to find a common unique field in those objects and use it as criteria for rearranging. |
| 44 | +You can disable this behaviour with `JsonDiff::SKIP_REARRANGE_ARRAY` option: |
| 45 | +```php |
| 46 | +$r = new JsonDiff( |
| 47 | + json_decode($originalJson), |
| 48 | + json_decode($newJson), |
| 49 | + JsonDiff::SKIP_REARRANGE_ARRAY |
| 50 | +); |
| 51 | +``` |
| 52 | + |
| 53 | +On created object you have several handy methods. |
| 54 | + |
| 55 | +### `getRearranged` |
| 56 | +Returns new value, rearranged with original order. |
| 57 | + |
| 58 | +### `getRemoved` |
| 59 | +Returns removals as partial value of original. |
| 60 | + |
| 61 | +### `getRemovedPaths` |
| 62 | +Returns list of `JSON` paths that were removed from original. |
| 63 | + |
| 64 | +### `getRemovedCnt` |
| 65 | +Returns number of removals. |
| 66 | + |
| 67 | +### `getAdded` |
| 68 | +Returns additions as partial value of new. |
| 69 | + |
| 70 | +### `getAddedPaths` |
| 71 | +Returns list of `JSON` paths that were added to new. |
| 72 | + |
| 73 | +### `getAddedCnt` |
| 74 | +Returns number of additions. |
| 75 | + |
| 76 | +### `getModifiedOriginal` |
| 77 | +Returns modifications as partial value of original. |
| 78 | + |
| 79 | +### `getModifiedNew` |
| 80 | +Returns modifications as partial value of new. |
| 81 | + |
| 82 | +### `getModifiedPaths` |
| 83 | +Returns list of `JSON` paths that were modified from original to new. |
| 84 | + |
| 85 | +### `getModifiedCnt` |
| 86 | +Returns number of modifications. |
| 87 | + |
| 88 | +## Example |
| 89 | + |
| 90 | +```php |
| 91 | +$originalJson = <<<'JSON' |
| 92 | +{ |
| 93 | + "key1": [4, 1, 2, 3], |
| 94 | + "key2": 2, |
| 95 | + "key3": { |
| 96 | + "sub0": 0, |
| 97 | + "sub1": "a", |
| 98 | + "sub2": "b" |
| 99 | + }, |
| 100 | + "key4": [ |
| 101 | + {"a":1, "b":true}, {"a":2, "b":false}, {"a":3} |
| 102 | + ] |
| 103 | +} |
| 104 | +JSON; |
| 105 | + |
| 106 | +$newJson = <<<'JSON' |
| 107 | +{ |
| 108 | + "key5": "wat", |
| 109 | + "key1": [5, 1, 2, 3], |
| 110 | + "key4": [ |
| 111 | + {"c":false, "a":2}, {"a":1, "b":true}, {"c":1, "a":3} |
| 112 | + ], |
| 113 | + "key3": { |
| 114 | + "sub3": 0, |
| 115 | + "sub2": false, |
| 116 | + "sub1": "c" |
| 117 | + } |
| 118 | +} |
| 119 | +JSON; |
| 120 | + |
| 121 | +$expected = <<<'JSON' |
| 122 | +{ |
| 123 | + "key1": [5, 1, 2, 3], |
| 124 | + "key3": { |
| 125 | + "sub1": "c", |
| 126 | + "sub2": false, |
| 127 | + "sub3": 0 |
| 128 | + }, |
| 129 | + "key4": [ |
| 130 | + {"a":1, "b":true}, {"a":2, "c":false}, {"a":3, "c":1} |
| 131 | + ], |
| 132 | + "key5": "wat" |
| 133 | +} |
| 134 | +JSON; |
| 135 | + |
| 136 | +$r = new JsonDiff(json_decode($originalJson), json_decode($newJson)); |
| 137 | +$this->assertSame( |
| 138 | + json_encode(json_decode($expected), JSON_PRETTY_PRINT), |
| 139 | + json_encode($r->getRearranged(), JSON_PRETTY_PRINT) |
| 140 | +); |
| 141 | +$this->assertSame('{"key3":{"sub3":0},"key4":{"1":{"c":false},"2":{"c":1}},"key5":"wat"}', |
| 142 | + json_encode($r->getAdded())); |
| 143 | +$this->assertSame(array( |
| 144 | + '#/key3/sub3', |
| 145 | + '#/key4/1/c', |
| 146 | + '#/key4/2/c', |
| 147 | + '#/key5', |
| 148 | +), $r->getAddedPaths()); |
| 149 | +$this->assertSame('{"key2":2,"key3":{"sub0":0},"key4":{"1":{"b":false}}}', |
| 150 | + json_encode($r->getRemoved())); |
| 151 | +$this->assertSame(array( |
| 152 | + '#/key2', |
| 153 | + '#/key3/sub0', |
| 154 | + '#/key4/1/b', |
| 155 | +), $r->getRemovedPaths()); |
| 156 | + |
| 157 | +$this->assertSame(array( |
| 158 | + '#/key1/0', |
| 159 | + '#/key3/sub1', |
| 160 | + '#/key3/sub2', |
| 161 | +), $r->getModifiedPaths()); |
| 162 | + |
| 163 | +$this->assertSame('{"key1":[4],"key3":{"sub1":"a","sub2":"b"}}', json_encode($r->getModifiedOriginal())); |
| 164 | +$this->assertSame('{"key1":[5],"key3":{"sub1":"c","sub2":false}}', json_encode($r->getModifiedNew())); |
| 165 | +``` |
| 166 | + |
| 167 | +## CLI tool |
| 168 | + |
| 169 | +### Usage |
| 170 | + |
| 171 | +``` |
| 172 | +json-diff --help |
| 173 | +v1.0.0 json-diff |
| 174 | +JSON diff and rearrange tool for PHP, https://github.com/swaggest/json-diff |
| 175 | +Usage: |
| 176 | + json-diff <action> <originalPath> <newPath> |
| 177 | + action Action to perform |
| 178 | + Allowed values: rearrange, changes, removals, additions, modifications |
| 179 | + originalPath Path to old (original) json file |
| 180 | + newPath Path to new json file |
| 181 | + |
| 182 | +Options: |
| 183 | + --out <out> Path to output result json file, STDOUT if not specified |
| 184 | + --show-paths Show JSON paths |
| 185 | + --show-json Show JSON result |
| 186 | + |
| 187 | +Misc: |
| 188 | + --help Show usage information |
| 189 | + --version Show version |
| 190 | + --bash-completion Generate bash completion |
| 191 | + --install Install to /usr/local/bin/ |
| 192 | +``` |
| 193 | + |
| 194 | +### Examples |
| 195 | + |
| 196 | +Using with standard `diff` |
| 197 | + |
| 198 | +``` |
| 199 | +json-diff rearrange ./composer.json ./composer2.json --show-json | diff ./composer.json - |
| 200 | +3c3 |
| 201 | +< "description": "JSON diff and merge tool for PHP", |
| 202 | +--- |
| 203 | +> "description": "JSON diff and merge tool for PHPH", |
| 204 | +5,11d4 |
| 205 | +< "license": "MIT", |
| 206 | +< "authors": [ |
| 207 | +< { |
| 208 | +< "name": "Viacheslav Poturaev", |
| 209 | + |
| 210 | +< } |
| 211 | +< ], |
| 212 | +25,28c18 |
| 213 | +< }, |
| 214 | +< "bin": [ |
| 215 | +< "bin/json-diff" |
| 216 | +< ] |
| 217 | +--- |
| 218 | +> } |
| 219 | +``` |
| 220 | + |
| 221 | +Showing differences in `JSON` mode |
| 222 | + |
| 223 | +``` |
| 224 | +bin/json-diff changes ./composer.json ./composer2.json --show-json --show-paths |
| 225 | +#/license |
| 226 | +#/authors |
| 227 | +#/bin |
| 228 | +#/description |
| 229 | +{ |
| 230 | + "removals": { |
| 231 | + "license": "MIT", |
| 232 | + "authors": [ |
| 233 | + { |
| 234 | + "name": "Viacheslav Poturaev", |
| 235 | + |
| 236 | + } |
| 237 | + ], |
| 238 | + "bin": [ |
| 239 | + "bin/json-diff" |
| 240 | + ] |
| 241 | + }, |
| 242 | + "additions": null, |
| 243 | + "modifiedOriginal": { |
| 244 | + "description": "JSON diff and merge tool for PHP" |
| 245 | + }, |
| 246 | + "modifiedNew": { |
| 247 | + "description": "JSON diff and merge tool for PHPH" |
| 248 | + } |
| 249 | +} |
| 250 | +``` |
0 commit comments