line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::JSON::Slurper; |
2
|
|
|
|
|
|
|
$File::JSON::Slurper::VERSION = '0.02'; |
3
|
2
|
|
|
2
|
|
3128
|
use 5.006; |
|
2
|
|
|
|
|
6
|
|
4
|
2
|
|
|
2
|
|
6
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
35
|
|
5
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
52
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
862
|
use parent 'Exporter'; |
|
2
|
|
|
|
|
632
|
|
|
2
|
|
|
|
|
10
|
|
8
|
2
|
|
|
2
|
|
946
|
use JSON::MaybeXS qw/ encode_json decode_json /; |
|
2
|
|
|
|
|
13461
|
|
|
2
|
|
|
|
|
124
|
|
9
|
2
|
|
|
2
|
|
967
|
use File::Slurper qw/ read_text write_text /; |
|
2
|
|
|
|
|
23772
|
|
|
2
|
|
|
|
|
307
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ read_json write_json /; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub read_json |
14
|
|
|
|
|
|
|
{ |
15
|
6
|
|
|
6
|
1
|
1380
|
my $json = read_text(@_); |
16
|
6
|
|
|
|
|
466
|
return decode_json($json); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub write_json |
20
|
|
|
|
|
|
|
{ |
21
|
3
|
|
|
3
|
1
|
987
|
my ($filename, $ref, @args) = @_; |
22
|
3
|
|
|
|
|
29
|
my $json = encode_json($ref); |
23
|
3
|
|
|
|
|
10
|
return write_text($filename, $json, @args); |
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, $encoding) |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Read JSON from C<$filename> and convert it to a Perl data structure. |
63
|
|
|
|
|
|
|
You'll get back either an arrayref or a hashref. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
You can optionally specify the C<$encoding> of the file, |
66
|
|
|
|
|
|
|
which defaults to UTF-8. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 write_json($filename, $ref, $encoding) |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Takes a Perl data structure C<$ref>, |
72
|
|
|
|
|
|
|
converts it to JSON and then writes it to file C<$filename>. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
You can optionally specify an C<$encoding>, |
75
|
|
|
|
|
|
|
which defaults to UTF-8. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 SEE ALSO |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
L provides a function L |
81
|
|
|
|
|
|
|
which is like the C function provided by this module. |
82
|
|
|
|
|
|
|
But you can't specify an encoding, |
83
|
|
|
|
|
|
|
and it doesn't provide a function for writing to a file as JSON. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
L is used to read and write files. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
L is used to encode and decode JSON. |
88
|
|
|
|
|
|
|
This itself is a front-end to the various JSON modules |
89
|
|
|
|
|
|
|
available on CPAN. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 REPOSITORY |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
L |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 AUTHOR |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Neil Bowers Eneilb@cpan.orgE |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This software is copyright (c) 2016 by Neil Bowers . |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
107
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |