I received a very confusing error from Mojo::JSON while my app was trying 
to parse a newly added config file using JSONConfig.  While I have figured 
it out, it raises a point worth mentioning, and possibly improving.

The error message was 

> Can't load application from file "/home/ubuntu/Origin/bin/origin": Can't 
> parse config "/home/ubuntu/Origin/origin.json": Malformed JSON: Expected 
> string while parsing object at line 6, offset 24 at 
> /home/ubuntu/perl5/perlbrew/perls/perl-5.20.0/lib/site_perl/5.20.0/Mojolicious/Plugin/JSONConfig.pm
>  
> line 10.
>

So, there's a problem on line 6, character 24.  But, here's what the config 
file was:

origin.json:
  1 {
  2     "databases": {
  3         "MODAPS": "Foo",
  4         "ACPS"  : "Bar"
  5     },
  6     %#Server secret, should be unique to each config.
  7     %#See http://mojolicious.org/perldoc/Mojolicious#secrets
  8     "secrets": "<%= $app->moniker %>",
  9 }


Line 6, character 24 is the 'o' in 'should' on a comment line.  What?  Even 
if I suppose it's disregarding comment lines, the 'p' in 'app' still looks 
OK.  What's more, changing line 8, even to a static string of the moniker, 
still had no effect.

I finally modified Mojo::JSON::_throw, to have it show me the actual error 
context as follows:
sub _throw {

  # Leading whitespace
  /\G[\x20\x09\x0a\x0d]*/gc;

  # Context
  my $context = 'Malformed JSON: ' . shift;
  if (m/\G\z/gc) { $context .= ' before end of data' }
  else {
    my @lines = split "\n", substr($_, 0, pos);
    my $popped;
    $context .= ' at line ' . @lines . ', offset ' . length($popped = pop 
@lines || '') . $popped;
  }

  die "$context\n";
}


This now gave me the error

> Can't load application from file "/home/ubuntu/Origin/bin/origin": Can't 
> parse config "/home/ubuntu/Origin/origin.json
> ": Malformed JSON: Expected string while parsing object at line 6, offset 
> 24    "secrets": "origin", at 
> /home/ubuntu/perl5/perlbrew/perls/perl-5.20.0/lib/site_perl/5.20.0/Mo
> jolicious/Plugin/JSONConfig.pm line 10.
>

At this point it became clear the issue was the trailing comma on line 8 of 
the config file that I'd completely overlooked.

Sri, et al, I'd like to suggest adding in the actual contextual line in 
_throw, similar to my quick hack, to help clarify exactly where a parsing 
error is coming from when comments and embedded perl are in play in the 
.json.

Best,
-Randall

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to