On Wed Sep 07 04:52:37 2011, LGODDARD wrote:
Show quoted text> If I am reading the documentation correctly, the following is the API
> for configuring the underlying parser:
>
> my $cfg = Config::Any->load_files({
> driver_args => {
> JSON => {
> driver_args => 1,
> }
> },
> use_ext => 1,
> files => [$CONFIG_FILE_PATH]
> });
Almost -- for it to be useful, driver_args should point to another
hashref:
driver_args => { foo => 'bar' }
Show quoted text> I have not delved into the code, but I presume the driver_args are
> passed to the JSON parser, which I found to be JSON::XS (is there an
API
Show quoted text> call to get that info, btw?).
Unfortunately, the JSON loader doesn't currently do anything with
driver_args.
Also, no, there isn't currently an API to see what parser will be used.
Show quoted text> The problem with the default configuration of JSON::XS is that it does
> not accept what it calls 'relaxed' JSON - that is JSON with trailing
commas:
This is odd, because as it turns out, "relaxed" is enabled already:
https://metacpan.org/source/BRICAS/Config-Any-
0.23/lib/Config/Any/JSON.pm#L62
Show quoted text> Would it be possible to extend the system to either provide access to
> the underlying parser object prior to calling 'load_files', or via a
> callback argument to 'load_files'?
I think it would be good to extend the loader to handle config options,
however, I think the issue you're reporting is unrelated due to the
default enabling of the "relaxed" setting.
FYI, the order of JSON plugins is:
1) JSON::DWIW
2) JSON::XS
3) JSON::Syck
4) JSON
So, please check to see if you have JSON::DWIW installed.
As an aside, here's a potential patch to handle args in the JSON loader:
diff --git a/lib/Config/Any/JSON.pm b/lib/Config/Any/JSON.pm
index ca2da3e..9774ca9 100644
--- a/lib/Config/Any/JSON.pm
+++ b/lib/Config/Any/JSON.pm
@@ -44,6 +44,7 @@ Attempts to load C<$file> as a JSON file.
sub load {
my $class = shift;
my $file = shift;
+ my $args = shift || {};
open( my $fh, $file ) or die $!;
my $content = do { local $/; <$fh> };
@@ -51,7 +52,7 @@ sub load {
eval { require JSON::DWIW; };
unless( $@ ) {
- my $decoder = JSON::DWIW->new;
+ my $decoder = JSON::DWIW->new( $args );
my ( $data, $error ) = $decoder->from_json( $content );
die $error if $error;
return $data;
@@ -60,6 +61,8 @@ sub load {
eval { require JSON::XS; };
unless( $@ ) {
my $decoder = JSON::XS->new->relaxed;
+ $decoder = $decoder->$_( $args->{ $_ } ) for keys %$args;
+
return $decoder->decode( $content );
}
@@ -70,7 +73,7 @@ sub load {
require JSON;
eval { JSON->VERSION( 2 ); };
- return $@ ? JSON::jsonToObj( $content ) : JSON::from_json( $content
);
+ return $@ ? JSON::jsonToObj( $content, $args ) : JSON::from_json(
$content, $args );
}
=head2 requires_any_of( )