3 package JSON::Diffable;
8 our $VERSION = '0.000001';
9 $VERSION = eval $VERSION;
11 my $real = JSON->new->relaxed->allow_nonref->utf8;
13 our @EXPORT_OK = qw( encode_json decode_json );
17 return _encode($data, 0);
22 return $real->decode($str);
33 if (ref $data eq 'HASH') {
34 return sprintf "{\n%s}",
37 my $key = $real->encode($_);
38 my $data = _encode($data->{$_});
39 _indent("$key: $data") . ",\n";
43 elsif (ref $data eq 'ARRAY') {
44 return sprintf "[\n%s]",
47 _indent(_encode($_)) . ",\n";
52 return $real->encode($data);
62 JSON::Diffable - A relaxed and easy diffable JSON variant
66 use JSON::Diffable qw( encode_json decode_json );
68 $json = encode_json $data;
69 $data = decode_json $json;
73 This module allows to create a JSON variant that is suitable for easy
78 =item * Commas after each hash or array element.
80 =item * Consistent indentation
82 =item * One line per entry
86 The data can be read again by a relaxed L<JSON> parser or the exported
87 L</decode_json> function.
93 my $json = encode_json($data);
95 Turns a Perl data structure into relaxed JSON.
99 my $data = decode_json($json);
101 Turns relaxed JSON into a Perl data structure.