File Coverage

blib/lib/Inline/JSON.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1              
2             package Inline::JSON;
3              
4 1     1   26920 use v5.10;
  1         5  
  1         52  
5 1     1   5 use strict;
  1         2  
  1         38  
6 1     1   5 use warnings;
  1         15  
  1         43  
7 1     1   6 no warnings 'once';
  1         2  
  1         34  
8              
9 1     1   1114 use Filter::Simple;
  1         36673  
  1         9  
10 1     1   1612 use JSON;
  1         17883  
  1         7  
11              
12             # This is using recursive balanced regex as per:
13             # http://learn.perl.org/faq/perlfaq6.html#Can-I-use-Perl-regular-expressions-to-match-balanced-text-
14             FILTER_ONLY
15             'executable' => sub {
16             s/
17             ( # Outer capture group 1 start
18             json: (\s*) # Capture group 2 (space) start
19             ( # Capture group 3 (braces) start
20             \{ # Opening brace
21             (?:
22             [^\{\}]++ # Non-braces, no backtrace
23             |
24             (?3) # Recursively capture group 2 (braces)
25             )*
26             \}
27             )
28             )
29             /
30             my $space = $2;
31             my $json = $3;
32             $json =~ s|'|\\'|;
33             $space."JSON->new->decode('$json')";
34             /gsex;
35             },
36             'executable' => sub {
37             s/
38             ( # Outer capture group 1 start
39             json: (\s*) # Capture group 2 (space) start
40             ( # Capture group 3 (brackets) start
41             \[ # Opening bracket
42             (?:
43             [^\[\]]++ # Non-brackets, no backtrace
44             |
45             (?3) # Recursively capture group 2 (brackets)
46             )*
47             \]
48             )
49             )
50             /
51             my $space = $2;
52             my $json = $3;
53             $json =~ s|'|\\'|;
54             $space."JSON->new->decode('$json')";
55             /gsex;
56             },
57             'all' => sub {
58             return unless $Inline::JSON::DEBUG;
59             print STDERR join '', map {"Inline::JSON> $_\n"} split /\n/, $_;
60             },
61             ;
62              
63              
64              
65             =head1 NAME
66              
67             Inline::JSON - Embed JSON data structures directly into your Perl code
68              
69             =cut
70              
71             our $VERSION = '1.0.4';
72              
73             =head1 SYNOPSIS
74              
75             use Inline::JSON;
76              
77             my $json = json: {
78             "name": "Awesome",
79             "title": "Mr.",
80             "skills": [
81             "Nunchucking",
82             "Bowhunting",
83             "Computer Hacking",
84             "Being Awesome",
85             ]
86             };
87              
88             use Data::Dumper;
89             print Dumper($json);
90              
91             Yields the output like:
92              
93             $VAR1 = {
94             'name' => 'Awesome',
95             'title' => 'Mr.',
96             'skills' => [
97             'Nunchucking',
98             'Bowhunting',
99             'Computer Hacking',
100             'Being Awesome',
101             ]
102             };
103              
104             You can also specify array references as the top-level JSON element, by using
105             brackets instead of curly braces:
106              
107             my $list_of_hashrefs = json: [
108             {
109             "id": "1",
110             "name": "one",
111             },
112             {
113             "id": "2",
114             "name": "two",
115             },
116             {
117             "id": "3",
118             "name": "three",
119             },
120             ];
121              
122             =head1 DESCRIPTION
123              
124             JSON is a data specification format used for interoperability with a
125             multitude of languages. Sometimes you have a chunk of JSON that you need to
126             turn into a Perl data structure. This module allows you to specify that
127             code inline within your perl program. It is syntactic sugar on top of
128             the existing JSON module, you could just as easily say:
129              
130             my $json = JSON->new->decode('{
131             // JSON code here
132             }');
133              
134             Which is what the module is doing internally, it just looks nicer.
135              
136             =head1 CAVEATS
137              
138             This module uses simple balanced brackets or curly braces matching to
139             determine the end of the chunk of JSON code, and does not pay attention to
140             quotes. If you have curly braces embedded in the strings in your JSON code,
141             it can cause the filter to misinterpret the end of the JSON.
142              
143             If you'd like to see what the filtered perl code looks like after the source
144             filter has been run on it, set the variable $Inline::JSON::DEBUG to a true
145             value prior to the 'use Inline::JSON' statement.
146              
147             =head1 SEE ALSO
148              
149             =over 8
150              
151             =item L
152              
153             The module being used to parse the JSON content.
154              
155             =item L
156              
157             The inspiration for this module.
158              
159             =back
160              
161              
162             =head1 AUTHOR
163              
164             Anthony Kilna, C<< >> - L
165              
166             =head1 BUGS
167              
168             Please report any bugs or feature requests to C, or through
169             the web interface at L. I will be notified, and then you'll
170             automatically be notified of progress on your bug as I make changes.
171              
172              
173              
174              
175             =head1 SUPPORT
176              
177             You can find documentation for this module with the perldoc command.
178              
179             perldoc Inline::JSON
180              
181              
182             You can also look for information at:
183              
184             =over 4
185              
186             =item * RT: CPAN's request tracker (report bugs here)
187              
188             L
189              
190             =item * AnnoCPAN: Annotated CPAN documentation
191              
192             L
193              
194             =item * CPAN Ratings
195              
196             L
197              
198             =item * Search CPAN
199              
200             L
201              
202             =back
203              
204              
205             =head1 ACKNOWLEDGEMENTS
206              
207              
208             =head1 LICENSE AND COPYRIGHT
209              
210             Copyright 2012 Kilna Companies.
211              
212             This program is free software; you can redistribute it and/or modify it
213             under the terms of either: the GNU General Public License as published
214             by the Free Software Foundation; or the Artistic License.
215              
216             See http://dev.perl.org/licenses/ for more information.
217              
218             =cut
219              
220             1; # End of Inline::JSON