Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Looking at JSON::Streaming::Reader, I quickly found this line:

my $char = $self->_peek_char();

which lead me to expect the module to be quite slow at what it does.

Luckily, parsing JSON is quite an easy task. Just checking it for validity is even easier than that. So I quickly rolled a JSON parser that just checked validity and didn't read the whole file into RAM at once.

So I built a 65MB file of JSON so I could compare:

> perl isJson.pl < file.json Valid JSON. Took 38s. > perl loops.pl okTook 446s.

So it looks like my version is over 10x faster.

[Update: Adding $jsonr->skip() inside the empty 'sub' as Loops suggests, makes the module run about 2x faster:

> perl skip.pl okTook 235s.

or only about 1/6th as fast as my code.]

Then I added an error near the end of the file:

> perl loops.pl panicTook 402s. > perl isJson.pl < file.json Invalid bare word (troo) at line 1020000, pos 8 (t) Took 39s.

So you can see it also gives much better information about where the problem was.

Here is the code:

#!/usr/bin/perl -w use strict; END { warn "Took ", time()-$^T, "s.\n"; } sub fail { my( $off, @msg ) = @_; my $line = $.; die @msg, " at line $line (end of file)\n" if ! defined $off; my $pos = pos $_ || 0; my $char = substr( $_, $pos+$off, 1 ); $pos++; die @msg, " at line $line, pos $pos ($char)\n"; } my $v1 = @ARGV ? 1 : 0; my %bare = qw< null 1 true 1 false 1 >; my $num = qr{ -? (?: 0 | [1-9][0-9]* ) (?: [.][0-9]+ )? (?: [eE][-+]?[0-9]+ )? }x; my @stack; my $end = '1'; while( <STDIN> ) { /\G\s+/gc; while( ! /\G$/gc ) { my $in = $stack[-1] || ''; if( '"' eq $end ) { while( /\G(?:[^\\"]+|\\(.))/gcs ) { ; } fail( -1, "Trailing \\ at end of file" ) if /\G\\$/gc; $end = ',' if /\G"/gc; } elsif( ':' eq $end ) { fail( 0, "Expected colon" ) if ! /\G:/gc; push @stack, ':'; $end = ''; } elsif( /\G([\]\}])/gc ) { my $got = $1; my $want = $end !~ /[,2]/ || ':' eq $in ? 'value' : $in || 'end of file'; fail( -1, "Found close bracket, $got, when expecting $want +" ) if $got ne $want; pop @stack; $end = ','; } elsif( ',' eq $end ) { fail( 0, "Expected end of file" ) if ! $in; fail( 0, "Expected comma" ) if ! /\G,/gc; $end = ''; } elsif( "\}" eq $in ) { fail( 0, "Expected string (key)" ) if ! /\G"/gc; $end = '"'; } elsif( /\G([\[\{])/gc ) { push @stack, {qw< [ ] { } >}->{$1}; $end = '2'; } elsif( '1' eq $end && $v1 ) { fail( 0, "JSON of just a scalar not allowed" ); } elsif( /\G"/gc ) { $end = '"'; } elsif( /\G$num/gc ) { $end = ','; } elsif( /\G([a-z]+)/gc ) { fail( -length($1), "Invalid bare word ($1)" ) if ! $bare{$1}; $end = ','; } else { fail( 0, "Expected value" ); } if( ',' eq $end && @stack ) { $end = ":" if "\}" eq $stack[-1]; pop @stack if ':' eq $stack[-1]; } /\G\s+/gc; } } fail( undef, "Empty JSON string" ) if 1 eq $end; fail( undef, "Unclosed string" ) if '"' eq $end; fail( undef, "Unclosed aggregate $stack[-1]" ) if @stack; warn "Valid JSON.\n";

- tye        


In reply to Re^3: Verify syntax of large JSON files (code) by tye
in thread Verify syntax of large JSON files by ron800

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-23 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found