Config::Any::JSON - Load JSON config files


Config-Any documentation Contained in the Config-Any distribution.

Index


Code Index:

NAME

Top

Config::Any::JSON - Load JSON config files

DESCRIPTION

Top

Loads JSON files. Example:

    {
        "name": "TestApp",
        "Controller::Foo": {
            "foo": "bar"
        },
        "Model::Baz": {
            "qux": "xyzzy"
        }
    }

METHODS

Top

extensions( )

return an array of valid extensions (json, jsn).

load( $file )

Attempts to load $file as a JSON file.

requires_any_of( )

Specifies that this modules requires one of, JSON::DWIW, JSON::XS, JSON::Syck or JSON in order to work.

AUTHOR

Top

Brian Cassidy <bricas@cpan.org>

COPYRIGHT AND LICENSE

Top

SEE ALSO

Top

* Catalyst
* Config::Any
* JSON::DWIW
* JSON::XS
* JSON::Syck
* JSON

Config-Any documentation Contained in the Config-Any distribution.
package Config::Any::JSON;

use strict;
use warnings;

use base 'Config::Any::Base';

sub extensions {
    return qw( json jsn );
}

sub load {
    my $class = shift;
    my $file  = shift;

    open( my $fh, $file ) or die $!;
    my $content = do { local $/; <$fh> };
    close $fh;

    eval { require JSON::DWIW; };
    unless( $@ ) {
        my $decoder = JSON::DWIW->new;
        my ( $data, $error ) = $decoder->from_json( $content );
        die $error if $error;
        return $data;
    }

    eval { require JSON::XS; };
    unless( $@ ) {
        my $decoder = JSON::XS->new->relaxed;
        return $decoder->decode( $content );
    }

    eval { require JSON::Syck; };
    unless( $@ ) {
        return JSON::Syck::Load( $content );
    }

    require JSON;
    eval { JSON->VERSION( 2 ); };
    return $@ ? JSON::jsonToObj( $content ) : JSON::from_json( $content );
}

sub requires_any_of { 'JSON::DWIW', 'JSON::XS', 'JSON::Syck', 'JSON' }

1;