Commit | Line | Data |
f0e3c221 |
1 | package Config::Any::JSON; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
dcfb1d1d |
6 | use base 'Config::Any::Base'; |
7 | |
f0e3c221 |
8 | =head1 NAME |
9 | |
10 | Config::Any::JSON - Load JSON config files |
11 | |
12 | =head1 DESCRIPTION |
13 | |
14 | Loads JSON files. Example: |
15 | |
16 | { |
17 | "name": "TestApp", |
18 | "Controller::Foo": { |
19 | "foo": "bar" |
20 | }, |
21 | "Model::Baz": { |
22 | "qux": "xyzzy" |
23 | } |
24 | } |
25 | |
26 | =head1 METHODS |
27 | |
28 | =head2 extensions( ) |
29 | |
30 | return an array of valid extensions (C<json>, C<jsn>). |
31 | |
32 | =cut |
33 | |
34 | sub extensions { |
35 | return qw( json jsn ); |
36 | } |
37 | |
38 | =head2 load( $file ) |
39 | |
40 | Attempts to load C<$file> as a JSON file. |
41 | |
42 | =cut |
43 | |
44 | sub load { |
45 | my $class = shift; |
46 | my $file = shift; |
47 | |
48 | open( my $fh, $file ) or die $!; |
49 | my $content = do { local $/; <$fh> }; |
50 | close $fh; |
51 | |
d49610ab |
52 | eval { require JSON::XS; }; |
53 | unless( $@ ) { |
54 | return JSON::XS::decode_json( $content ); |
f0e3c221 |
55 | } |
d49610ab |
56 | |
57 | eval { require JSON::Syck; }; |
58 | unless( $@ ) { |
f0e3c221 |
59 | return JSON::Syck::Load( $content ); |
60 | } |
d49610ab |
61 | |
62 | require JSON; |
63 | eval { JSON->VERSION( 2 ); }; |
64 | return $@ ? JSON::jsonToObj( $content ) : JSON::from_json( $content ); |
f0e3c221 |
65 | } |
66 | |
dcfb1d1d |
67 | =head2 requires_any_of( ) |
72628dc7 |
68 | |
d49610ab |
69 | Specifies that this modules requires one of, L<JSON::XS>, L<JSON::Syck> or |
70 | L<JSON> in order to work. |
72628dc7 |
71 | |
72 | =cut |
73 | |
d49610ab |
74 | sub requires_any_of { 'JSON::XS', 'JSON::Syck', 'JSON' } |
72628dc7 |
75 | |
f0e3c221 |
76 | =head1 AUTHOR |
77 | |
a918b0b8 |
78 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> |
f0e3c221 |
79 | |
80 | =head1 COPYRIGHT AND LICENSE |
81 | |
a918b0b8 |
82 | Copyright 2007 by Brian Cassidy |
f0e3c221 |
83 | |
84 | This library is free software; you can redistribute it and/or modify |
85 | it under the same terms as Perl itself. |
86 | |
87 | =head1 SEE ALSO |
88 | |
89 | =over 4 |
90 | |
91 | =item * L<Catalyst> |
92 | |
93 | =item * L<Config::Any> |
94 | |
95 | =item * L<JSON> |
96 | |
97 | =item * L<JSON::Syck> |
98 | |
d49610ab |
99 | =item * L<JSON::XS> |
100 | |
f0e3c221 |
101 | =back |
102 | |
103 | =cut |
104 | |
105 | 1; |