summaryrefslogtreecommitdiff
path: root/lib/Carton/Util.pm
blob: cc9c77558de26459c840ad025889ddd77c54952f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;