3 # lots of this stuff was sponsored by socialflow.com
5 package JSON::Diffable;
10 our $VERSION = '0.000001';
11 $VERSION = eval $VERSION;
13 my $real = JSON->new->relaxed->allow_nonref->utf8;
15 our @EXPORT_OK = qw( encode_json decode_json );
19 return _encode($data, 0);
24 return $real->decode($str);
35 if (ref $data eq 'HASH') {
36 return sprintf "{\n%s}",
39 my $key = $real->encode($_);
40 my $data = _encode($data->{$_});
41 _indent("$key: $data") . ",\n";
45 elsif (ref $data eq 'ARRAY') {
46 return sprintf "[\n%s]",
49 _indent(_encode($_)) . ",\n";
54 return $real->encode($data);
64 JSON::Diffable - A relaxed and easy diffable JSON variant
68 use JSON::Diffable qw( encode_json decode_json );
70 $json = encode_json $data;
71 $data = decode_json $json;
75 This module allows to create a JSON variant that is suitable for easy
80 =item * Commas after each hash or array element.
82 =item * Consistent indentation
84 =item * One line per entry
88 The data can be read again by a relaxed L<JSON> parser or the exported
89 L</decode_json> function.
95 my $json = encode_json($data);
97 Turns a Perl data structure into relaxed JSON.
101 my $data = decode_json($json);
103 Turns relaxed JSON into a Perl data structure.
107 The development of this module was sponsored by L<http://socialflow.com/>.