1 package Config::Any::JSON;
6 use base 'Config::Any::Base';
10 Config::Any::JSON - Load JSON config files
14 Loads JSON files. Example:
30 return an array of valid extensions (C<json>, C<jsn>).
35 return qw( json jsn );
40 Attempts to load C<$file> as a JSON file.
48 open( my $fh, '<', $file ) or die $!;
50 my $content = do { local $/; <$fh> };
53 if ( eval { require Cpanel::JSON::XS } ) {
54 my $decoder = Cpanel::JSON::XS->new->utf8->relaxed;
55 return $decoder->decode( $content );
57 elsif ( eval { require JSON::MaybeXS } ) {
58 my $decoder = JSON::MaybeXS::JSON()->new->utf8->relaxed;
59 return $decoder->decode( $content );
61 elsif ( eval { require JSON::DWIW } ) {
62 my $decoder = JSON::DWIW->new;
63 my ( $data, $error ) = $decoder->from_json( $content );
67 elsif ( eval { require JSON::XS } ) {
68 my $decoder = JSON::XS->new->utf8->relaxed;
69 return $decoder->decode( $content );
71 elsif ( eval { require JSON::Syck } ) {
73 return JSON::Syck::Load( Encode::decode('UTF-8', $content ) );
75 elsif ( eval { require JSON::PP; JSON::PP->VERSION( 2 ); } ) {
76 my $decoder = JSON::PP->new->utf8->relaxed;
77 return $decoder->decode( $content );
80 if ( eval { JSON->VERSION( 2 ) } ) {
81 return JSON::decode_json( $content );
84 return JSON::jsonToObj( $content );
88 =head2 requires_any_of( )
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.
96 sub requires_any_of { qw(
108 Brian Cassidy <bricas@cpan.org>
110 =head1 COPYRIGHT AND LICENSE
112 Copyright 2006-2016 by Brian Cassidy
114 This library is free software; you can redistribute it and/or modify
115 it under the same terms as Perl itself.
123 =item * L<Config::Any>
125 =item * L<Cpanel::JSON::XS>
127 =item * L<JSON::MaybeXS>
129 =item * L<JSON::DWIW>
133 =item * L<JSON::Syck>