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 | |
3a27e96d |
48 | open( my $fh, '<', $file ) or die $!; |
49 | binmode $fh; |
f0e3c221 |
50 | my $content = do { local $/; <$fh> }; |
51 | close $fh; |
52 | |
837fdc4c |
53 | if ( eval { require Cpanel::JSON::XS } ) { |
54 | my $decoder = Cpanel::JSON::XS->new->utf8->relaxed; |
55 | return $decoder->decode( $content ); |
56 | } |
57 | elsif ( eval { require JSON::MaybeXS } ) { |
58 | my $decoder = JSON::MaybeXS::JSON()->new->utf8->relaxed; |
59 | return $decoder->decode( $content ); |
60 | } |
61 | elsif ( eval { require JSON::DWIW } ) { |
49ae6583 |
62 | my $decoder = JSON::DWIW->new; |
63 | my ( $data, $error ) = $decoder->from_json( $content ); |
64 | die $error if $error; |
65 | return $data; |
66 | } |
4473d906 |
67 | elsif ( eval { require JSON::XS } ) { |
3a27e96d |
68 | my $decoder = JSON::XS->new->utf8->relaxed; |
49ae6583 |
69 | return $decoder->decode( $content ); |
f0e3c221 |
70 | } |
4473d906 |
71 | elsif ( eval { require JSON::Syck } ) { |
3a27e96d |
72 | require Encode; |
73 | return JSON::Syck::Load( Encode::decode('UTF-8', $content ) ); |
f0e3c221 |
74 | } |
4473d906 |
75 | elsif ( eval { require JSON::PP; JSON::PP->VERSION( 2 ); } ) { |
3a27e96d |
76 | my $decoder = JSON::PP->new->utf8->relaxed; |
a8ffcd3b |
77 | return $decoder->decode( $content ); |
78 | } |
d49610ab |
79 | require JSON; |
4473d906 |
80 | if ( eval { JSON->VERSION( 2 ) } ) { |
3a27e96d |
81 | return JSON::decode_json( $content ); |
4473d906 |
82 | } |
83 | else { |
84 | return JSON::jsonToObj( $content ); |
85 | } |
f0e3c221 |
86 | } |
87 | |
dcfb1d1d |
88 | =head2 requires_any_of( ) |
72628dc7 |
89 | |
d60679ba |
90 | Specifies that this modules requires one of, L<Cpanel::JSON::XS>, |
91 | L<JSON::MaybeXS>, L<JSON::DWIW>, L<JSON::XS>, L<JSON::Syck>, L<JSON::PP> or |
92 | L<JSON> in order to work. |
72628dc7 |
93 | |
94 | =cut |
95 | |
d60679ba |
96 | sub requires_any_of { qw( |
97 | Cpanel::JSON::XS |
98 | JSON::MaybeXS |
99 | JSON::DWIW |
100 | JSON::XS |
101 | JSON::Syck |
102 | JSON::PP |
103 | JSON |
104 | ) } |
72628dc7 |
105 | |
f0e3c221 |
106 | =head1 AUTHOR |
107 | |
20190ef7 |
108 | Brian Cassidy <bricas@cpan.org> |
f0e3c221 |
109 | |
110 | =head1 COPYRIGHT AND LICENSE |
111 | |
3d43e104 |
112 | Copyright 2006-2016 by Brian Cassidy |
f0e3c221 |
113 | |
114 | This library is free software; you can redistribute it and/or modify |
19f6cb63 |
115 | it under the same terms as Perl itself. |
f0e3c221 |
116 | |
117 | =head1 SEE ALSO |
118 | |
19f6cb63 |
119 | =over 4 |
f0e3c221 |
120 | |
121 | =item * L<Catalyst> |
122 | |
123 | =item * L<Config::Any> |
124 | |
20190ef7 |
125 | =item * L<Cpanel::JSON::XS> |
126 | |
127 | =item * L<JSON::MaybeXS> |
128 | |
49ae6583 |
129 | =item * L<JSON::DWIW> |
130 | |
131 | =item * L<JSON::XS> |
f0e3c221 |
132 | |
133 | =item * L<JSON::Syck> |
134 | |
49ae6583 |
135 | =item * L<JSON> |
d49610ab |
136 | |
f0e3c221 |
137 | =back |
138 | |
139 | =cut |
140 | |
141 | 1; |