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 | |
4473d906 |
52 | if ( eval { require JSON::DWIW } ) { |
49ae6583 |
53 | my $decoder = JSON::DWIW->new; |
54 | my ( $data, $error ) = $decoder->from_json( $content ); |
55 | die $error if $error; |
56 | return $data; |
57 | } |
4473d906 |
58 | elsif ( eval { require JSON::XS } ) { |
49ae6583 |
59 | my $decoder = JSON::XS->new->relaxed; |
60 | return $decoder->decode( $content ); |
f0e3c221 |
61 | } |
4473d906 |
62 | elsif ( eval { require JSON::Syck } ) { |
f0e3c221 |
63 | return JSON::Syck::Load( $content ); |
64 | } |
4473d906 |
65 | elsif ( eval { require JSON::PP; JSON::PP->VERSION( 2 ); } ) { |
a8ffcd3b |
66 | my $decoder = JSON::PP->new->relaxed; |
67 | return $decoder->decode( $content ); |
68 | } |
d49610ab |
69 | require JSON; |
4473d906 |
70 | if ( eval { JSON->VERSION( 2 ) } ) { |
71 | return JSON::from_json( $content ); |
72 | } |
73 | else { |
74 | return JSON::jsonToObj( $content ); |
75 | } |
f0e3c221 |
76 | } |
77 | |
dcfb1d1d |
78 | =head2 requires_any_of( ) |
72628dc7 |
79 | |
49ae6583 |
80 | Specifies that this modules requires one of, L<JSON::DWIW>, L<JSON::XS>, |
81 | L<JSON::Syck> or L<JSON> in order to work. |
72628dc7 |
82 | |
83 | =cut |
84 | |
a8ffcd3b |
85 | sub requires_any_of { 'JSON::DWIW', 'JSON::XS', 'JSON::Syck', 'JSON::PP', 'JSON' } |
72628dc7 |
86 | |
f0e3c221 |
87 | =head1 AUTHOR |
88 | |
a918b0b8 |
89 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> |
f0e3c221 |
90 | |
91 | =head1 COPYRIGHT AND LICENSE |
92 | |
3d43e104 |
93 | Copyright 2006-2016 by Brian Cassidy |
f0e3c221 |
94 | |
95 | This library is free software; you can redistribute it and/or modify |
96 | it under the same terms as Perl itself. |
97 | |
98 | =head1 SEE ALSO |
99 | |
100 | =over 4 |
101 | |
102 | =item * L<Catalyst> |
103 | |
104 | =item * L<Config::Any> |
105 | |
49ae6583 |
106 | =item * L<JSON::DWIW> |
107 | |
108 | =item * L<JSON::XS> |
f0e3c221 |
109 | |
110 | =item * L<JSON::Syck> |
111 | |
49ae6583 |
112 | =item * L<JSON> |
d49610ab |
113 | |
f0e3c221 |
114 | =back |
115 | |
116 | =cut |
117 | |
118 | 1; |