6c6eb6e20206215cf5476731139c4d3cbd25b37a
[p5sagit/Config-Any.git] / lib / Config / Any / JSON.pm
1 package Config::Any::JSON;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Config::Any::JSON - Load JSON config files
9
10 =head1 DESCRIPTION
11
12 Loads JSON files. Example:
13
14     {
15         "name": "TestApp",
16         "Controller::Foo": {
17             "foo": "bar"
18         },
19         "Model::Baz": {
20             "qux": "xyzzy"
21         }
22     }
23
24 =head1 METHODS
25
26 =head2 extensions( )
27
28 return an array of valid extensions (C<json>, C<jsn>).
29
30 =cut
31
32 sub extensions {
33     return qw( json jsn );
34 }
35
36 =head2 load( $file )
37
38 Attempts to load C<$file> as a JSON file.
39
40 =cut
41
42 sub load {
43     my $class = shift;
44     my $file  = shift;
45
46     open( my $fh, $file ) or die $!;
47     my $content = do { local $/; <$fh> };
48     close $fh;
49
50     eval { require JSON::Syck; };
51     if ( $@ ) {
52         require JSON;
53         eval { JSON->VERSION( 2 ); };
54         return $@ ? JSON::jsonToObj( $content ) : JSON::from_json( $content );
55     }
56     else {
57         return JSON::Syck::Load( $content );
58     }
59 }
60
61 =head2 is_supported( )
62
63 Returns true if either L<JSON::Syck> or L<JSON> is available.
64
65 =cut
66
67 sub is_supported {
68     eval { require JSON::Syck; };
69     return 1 unless $@;
70     eval { require JSON; };
71     return $@ ? 0 : 1;
72 }
73
74 =head1 AUTHOR
75
76 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
77
78 =head1 COPYRIGHT AND LICENSE
79
80 Copyright 2007 by Brian Cassidy
81
82 This library is free software; you can redistribute it and/or modify
83 it under the same terms as Perl itself. 
84
85 =head1 SEE ALSO
86
87 =over 4 
88
89 =item * L<Catalyst>
90
91 =item * L<Config::Any>
92
93 =item * L<JSON>
94
95 =item * L<JSON::Syck>
96
97 =back
98
99 =cut
100
101 1;