diff options
Diffstat (limited to 'lib/Carton/Util.pm')
-rw-r--r-- | lib/Carton/Util.pm | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/Carton/Util.pm b/lib/Carton/Util.pm new file mode 100644 index 0000000..cc9c775 --- /dev/null +++ b/lib/Carton/Util.pm @@ -0,0 +1,31 @@ +package Carton::Util; +use strict; +use warnings; + +sub load_json { + my $file = shift; + + open my $fh, "<", $file or die "$file: $!"; + from_json(join '', <$fh>); +} + +sub dump_json { + my($data, $file) = @_; + + open my $fh, ">", $file or die "$file: $!"; + binmode $fh; + print $fh to_json($data); +} + +sub from_json { + require JSON; + JSON::decode_json(@_); +} + +sub to_json { + my($data) = @_; + require JSON; + JSON->new->utf8->pretty->canonical->encode($data); +} + +1; |