File Coverage

blib/lib/File/JSON/Slurper.pm
Criterion Covered Total %
statement 22 22 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 32 32 100.0


line stmt bran cond sub pod time code
1             package File::JSON::Slurper;
2             $File::JSON::Slurper::VERSION = '1.00';
3 2     2   3410 use 5.006;
  2         12  
4 2     2   11 use strict;
  2         2  
  2         68  
5 2     2   10 use warnings;
  2         4  
  2         72  
6              
7 2     2   946 use parent 'Exporter';
  2         609  
  2         10  
8 2     2   913 use JSON::MaybeXS qw/ encode_json decode_json /;
  2         14973  
  2         115  
9 2     2   876 use File::Slurper qw/ read_binary write_binary /;
  2         27188  
  2         347  
10              
11             our @EXPORT_OK = qw/ read_json write_json /;
12              
13             sub read_json
14             {
15 6     6 1 2365 my $json = read_binary(@_);
16 6         482 return decode_json($json);
17             }
18              
19             sub write_json
20             {
21 3     3 1 1195 my ($filename, $ref) = @_;
22 3         18 my $json = encode_json($ref);
23 3         10 return write_binary($filename, $json);
24             }
25              
26             1;
27              
28             =head1 NAME
29              
30             File::JSON::Slurper - slurp a JSON file into a data structure, and the reverse
31              
32             =head1 SYNOPSIS
33              
34             use File::JSON::Slurper qw/ read_json write_json /;
35              
36             my $ref = read_json('stuff.json');
37              
38             my $data = { name => 'NEILB', age => 21 };
39             write_json('fibber.json', $data);
40              
41              
42             =head1 DESCRIPTION
43              
44             This module provides two functions for getting Perl data structures
45             into and out of files in JSON format.
46             One will read a Perl data structure from a JSON file,
47             and the other will take a Perl data structure and write it to a file
48             as JSON.
49              
50             I wrote this module because I kept finding myself using
51             L to read JSON from a file,
52             and then L to convert the JSON to a Perl data structure.
53              
54             No functions are exported by default,
55             you must specify which function(s) you want to import.
56              
57              
58             =head1 FUNCTIONS
59              
60             =head2 read_json($filename)
61              
62             Read JSON from C<$filename> as UTF-8
63             and convert it to a Perl data structure.
64             You'll get back either an arrayref or a hashref.
65              
66              
67             =head2 write_json($filename, $ref)
68              
69             Takes a Perl data structure C<$ref>,
70             converts it to JSON and then writes it to file C<$filename>
71             as UTF-8.
72              
73              
74             =head1 SEE ALSO
75              
76             L provides a function L
77             which is like the C function provided by this module.
78             But you can't specify an encoding,
79             and it doesn't provide a function for writing to a file as JSON.
80              
81             L is used to read and write files.
82              
83             L is used to encode and decode JSON.
84             This itself is a front-end to the various JSON modules
85             available on CPAN.
86              
87              
88             =head1 REPOSITORY
89              
90             L
91              
92              
93             =head1 AUTHOR
94              
95             Neil Bowers Eneilb@cpan.orgE
96              
97              
98             =head1 COPYRIGHT AND LICENSE
99              
100             This software is copyright (c) 2016 by Neil Bowers .
101              
102             This is free software; you can redistribute it and/or modify it under
103             the same terms as the Perl 5 programming language system itself.
104              
105             =cut