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 JSON::DWIW } ) {
54 my $decoder = JSON::DWIW->new;
55 my ( $data, $error ) = $decoder->from_json( $content );
59 elsif ( eval { require JSON::XS } ) {
60 my $decoder = JSON::XS->new->utf8->relaxed;
61 return $decoder->decode( $content );
63 elsif ( eval { require JSON::Syck } ) {
65 return JSON::Syck::Load( Encode::decode('UTF-8', $content ) );
67 elsif ( eval { require JSON::PP; JSON::PP->VERSION( 2 ); } ) {
68 my $decoder = JSON::PP->new->utf8->relaxed;
69 return $decoder->decode( $content );
72 if ( eval { JSON->VERSION( 2 ) } ) {
73 return JSON::decode_json( $content );
76 return JSON::jsonToObj( $content );
80 =head2 requires_any_of( )
82 Specifies that this modules requires one of, L<JSON::DWIW>, L<JSON::XS>,
83 L<JSON::Syck> or L<JSON> in order to work.
87 sub requires_any_of { 'JSON::DWIW', 'JSON::XS', 'JSON::Syck', 'JSON::PP', 'JSON' }
91 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
93 =head1 COPYRIGHT AND LICENSE
95 Copyright 2006-2016 by Brian Cassidy
97 This library is free software; you can redistribute it and/or modify
98 it under the same terms as Perl itself.
106 =item * L<Config::Any>
108 =item * L<JSON::DWIW>
112 =item * L<JSON::Syck>