File Coverage

blib/lib/Config/Any/JSON.pm
Criterion Covered Total %
statement 18 21 85.7
branch 2 4 50.0
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 26 31 83.9


line stmt bran cond sub pod time code
1             package Config::Any::JSON;
2              
3 6     6   153 use strict;
  6         54  
  6         89  
4 6     6   89 use warnings;
  6         54  
  6         92  
5              
6             =head1 NAME
7            
8             Config::Any::JSON - Load JSON config files
9            
10             =head1 DESCRIPTION
11            
12             Loads JSON files. Example:
13            
14             {
15             "name": "TestApp",
16             "Controller::Foo": {
17             "foo": "bar"
18             },
19             "Model::Baz": {
20             "qux": "xyzzy"
21             }
22             }
23            
24             =head1 METHODS
25            
26             =head2 extensions( )
27            
28             return an array of valid extensions (C<json>, C<jsn>).
29            
30             =cut
31              
32             sub extensions {
33 9     9 1 117     return qw( json jsn );
34             }
35              
36             =head2 load( $file )
37            
38             Attempts to load C<$file> as a JSON file.
39            
40             =cut
41              
42             sub load {
43 6     6 1 5604     my $class = shift;
44 6         83     my $file = shift;
45              
46 6 50       556     open( my $fh, $file ) or die $!;
47 6         56     my $content = do { local $/; <$fh> };
  6         73  
  6         3052  
48 6         134     close $fh;
49              
50 6         54     eval { require JSON::Syck; };
  6         172  
51 6 50       94     if( $@ ) {
52 0         0         require JSON;
53 0         0         JSON->import;
54 0         0         return jsonToObj( $content );
55                 }
56                 else {
57 6         408         return JSON::Syck::Load( $content );
58                 }
59             }
60              
61             =head1 AUTHOR
62            
63             =over 4
64            
65             =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
66            
67             =back
68            
69             =head1 COPYRIGHT AND LICENSE
70            
71             Copyright 2006 by Brian Cassidy
72            
73             This library is free software; you can redistribute it and/or modify
74             it under the same terms as Perl itself.
75            
76             =head1 SEE ALSO
77            
78             =over 4
79            
80             =item * L<Catalyst>
81            
82             =item * L<Config::Any>
83            
84             =item * L<JSON>
85            
86             =item * L<JSON::Syck>
87            
88             =back
89            
90             =cut
91              
92             1;