X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FJSON%2FDiffable.pm;h=c9cef45a25426c18eeaa9ba84bd2144179cc6184;hb=381475799f04145f9eb980eaee063d72b2b7c680;hp=7530d9c2e5c904758dd69ccb95bc6aa1f8a4d85c;hpb=dd1bf37158da1c569f745f980a5b970aaf455b69;p=scpubgit%2FJSON-Diffable.git diff --git a/lib/JSON/Diffable.pm b/lib/JSON/Diffable.pm index 7530d9c..c9cef45 100644 --- a/lib/JSON/Diffable.pm +++ b/lib/JSON/Diffable.pm @@ -1,19 +1,29 @@ use strictures 1; +# lots of this stuff was sponsored by socialflow.com + package JSON::Diffable; use JSON (); use Exporter 'import'; -my $real = JSON->new->allow_nonref->utf8; +our $VERSION = '0.000001'; +$VERSION = eval $VERSION; + +my $real = JSON->new->relaxed->allow_nonref->utf8; -our @EXPORT_OK = qw( encode_json ); +our @EXPORT_OK = qw( encode_json decode_json ); sub encode_json { my $data = shift; return _encode($data, 0); } +sub decode_json { + my $str = shift; + return $real->decode($str); +} + sub _indent { my $str = shift; $str =~ s{^}{ }gm; @@ -46,3 +56,54 @@ sub _encode { } 1; + +__END__ + +=head1 NAME + +JSON::Diffable - A relaxed and easy diffable JSON variant + +=head1 SYNOPSIS + + use JSON::Diffable qw( encode_json decode_json ); + + $json = encode_json $data; + $data = decode_json $json; + +=head1 DESCRIPTION + +This module allows to create a JSON variant that is suitable for easy +diffing. This means: + +=over + +=item * Commas after each hash or array element. + +=item * Consistent indentation + +=item * One line per entry + +=back + +The data can be read again by a relaxed L parser or the exported +L function. + +=head1 EXPORTS + +=head2 encode_json + + my $json = encode_json($data); + +Turns a Perl data structure into relaxed JSON. + +=head2 decode_json + + my $data = decode_json($json); + +Turns relaxed JSON into a Perl data structure. + +=head1 SPONSORED + +The development of this module was sponsored by L. + +=cut