3 # lots of this stuff was sponsored by socialflow.com
5 package JSON::Diffable;
10 our $VERSION = '0.000001'; # 0.0.1
12 $VERSION = eval $VERSION;
14 my $real = JSON->new->relaxed->allow_nonref->utf8;
16 our @EXPORT_OK = qw( encode_json decode_json );
20 return _encode($data, 0);
25 return $real->decode($str);
36 if (ref $data eq 'HASH') {
37 return sprintf "{\n%s}",
40 my $key = $real->encode($_);
41 my $data = _encode($data->{$_});
42 _indent("$key: $data") . ",\n";
46 elsif (ref $data eq 'ARRAY') {
47 return sprintf "[\n%s]",
50 _indent(_encode($_)) . ",\n";
55 return $real->encode($data);
65 JSON::Diffable - A relaxed and easy diffable JSON variant
69 use JSON::Diffable qw( encode_json decode_json );
71 $json = encode_json $data;
72 $data = decode_json $json;
76 This module allows to create a JSON variant that is suitable for easy
81 =item * Commas after each hash or array element.
83 =item * Consistent indentation
85 =item * One line per entry
89 The data can be read again by a relaxed L<JSON> parser or the exported
90 L</decode_json> function.
96 my $json = encode_json($data);
98 Turns a Perl data structure into diffable JSON.
102 my $data = decode_json($json);
104 Turns relaxed JSON into a Perl data structure.
108 Robert Sedlacek <r.sedlacek@shadowcat.co.uk>
112 The development of this module was sponsored by L<http://socialflow.com/>.