| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Algorithm::Diff::JSON; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 489 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 652 | use Algorithm::Diff qw(diff); | 
|  | 1 |  |  |  |  | 5676 |  | 
|  | 1 |  |  |  |  | 139 |  | 
| 7 | 1 |  |  | 1 |  | 1030 | use Cpanel::JSON::XS qw(encode_json); | 
|  | 1 |  |  |  |  | 6001 |  | 
|  | 1 |  |  |  |  | 67 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 571 | use Sub::Exporter -setup  => { exports => [ 'json_diff' ] }; | 
|  | 1 |  |  |  |  | 12276 |  | 
|  | 1 |  |  |  |  | 10 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '1.000'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub json_diff { | 
| 14 | 4 |  |  | 4 | 1 | 300 | my @changes = (); | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 4 |  |  |  |  | 19 | foreach my $diff (map { @{$_} } diff(@_)) { | 
|  | 12 |  |  |  |  | 3885 |  | 
|  | 12 |  |  |  |  | 28 |  | 
| 17 | 21 |  |  |  |  | 29 | my($action, $this_line, $content) = @{$diff}; | 
|  | 21 |  |  |  |  | 37 |  | 
| 18 | 21 | 100 |  |  |  | 58 | if(defined($changes[$this_line])) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | $changes[$this_line] = { | 
| 20 |  |  |  |  |  |  | change => { | 
| 21 |  |  |  |  |  |  | add    => $content, | 
| 22 |  |  |  |  |  |  | remove => $changes[$this_line]->{remove} | 
| 23 |  |  |  |  |  |  | } | 
| 24 | 6 |  |  |  |  | 24 | }; | 
| 25 |  |  |  |  |  |  | } elsif($action eq '+') { | 
| 26 | 5 |  |  |  |  | 18 | $changes[$this_line] = { add  => $content }; | 
| 27 |  |  |  |  |  |  | } elsif($action eq '-') { | 
| 28 | 10 |  |  |  |  | 25 | $changes[$this_line] = { remove => $content }; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | return encode_json([ | 
| 32 | 4 | 100 |  |  |  | 18 | map { defined($changes[$_]) ? { element => $_, %{$changes[$_]} } : () } | 
|  | 93 |  |  |  |  | 146 |  | 
|  | 15 |  |  |  |  | 172 |  | 
| 33 |  |  |  |  |  |  | 0 .. $#changes | 
| 34 |  |  |  |  |  |  | ]); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =head1 NAME | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | Algorithm::Diff::JSON - find the differences between two lists and report on them in JSON | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | This perl code: | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | use Algorithm::Diff::JSON qw(json_diff); | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | my $json = json_diff( | 
| 48 |  |  |  |  |  |  | [0,      1, 2, 3, 4, 5,      6], | 
| 49 |  |  |  |  |  |  | ['zero', 1, 2, 3,    5, 5.5, 6] | 
| 50 |  |  |  |  |  |  | ); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | will generate this JSON: | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | [ | 
| 55 |  |  |  |  |  |  | { "element": 0, "change": { "remove": 0, "add": "zero" } }, | 
| 56 |  |  |  |  |  |  | { "element": 4, "remove": 4 }, | 
| 57 |  |  |  |  |  |  | { "element": 5, "add": 5.5 } | 
| 58 |  |  |  |  |  |  | ] | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | (well, an ugly, minimised, equivalent version of that JSON anyway) | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | There is only one function, which is a simple wrapper around L's | 
| 65 |  |  |  |  |  |  | C function: | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =head2 json_diff | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | This takes two list-ref arguments. It returns a JSON array describing the | 
| 70 |  |  |  |  |  |  | changes needed to transform the first into the second. | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | This function may be exported. If you want to export it with a different name | 
| 73 |  |  |  |  |  |  | then you can do so: | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | use Algorithm::Diff::JSON 'json_diff' => { -as => 'something_else }; | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | Each element in the returned array is a hash. Hashes always have: | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =over | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =item element | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | The element number, as given to us by C | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =back | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | and will also have exactly one of the following keys: | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =over | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =item add | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | The content to add at this location | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =item remove | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | The content to remove from this location | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =item change | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | A hash of both ... | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =over | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =item add | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | The content to add at this location | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =item remove | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | The content which that replaces at this location | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =back | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =back | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head1 FEEDBACK | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | I welcome feedback about my code, including constructive criticism, bug | 
| 120 |  |  |  |  |  |  | reports, documentation improvements, and feature requests. The best bug reports | 
| 121 |  |  |  |  |  |  | include files that I can add to the test suite, which fail with the current | 
| 122 |  |  |  |  |  |  | code in my git repo and will pass once I've fixed the bug | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Feature requests are far more likely to get implemented if you submit a patch | 
| 125 |  |  |  |  |  |  | yourself. | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =head1 SOURCE CODE REPOSITORY | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | L | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | L | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | L | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =head1 AUTHOR, LICENCE and COPYRIGHT | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | Copyright 2020 David Cantrell EFE | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | This software is free-as-in-speech software, and may be used, distributed, and | 
| 142 |  |  |  |  |  |  | modified under the terms of either the GNU General Public Licence version 2 or | 
| 143 |  |  |  |  |  |  | the Artistic Licence. It's up to you which one you use. The full text of the | 
| 144 |  |  |  |  |  |  | licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively. | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =head1 CONSPIRACY | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | This module is also free-as-in-mason software. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =cut | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | 1; |