Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Verify syntax of large JSON files (code)

by tye (Sage)
on Nov 23, 2014 at 22:58 UTC ( [id://1108191]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Verify syntax of large JSON files
in thread Verify syntax of large JSON files

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        

Replies are listed 'Best First'.
Re^4: Verify syntax of large JSON files (code)
by graff (Chancellor) on Nov 24, 2014 at 06:31 UTC
    I decided I had to front-page this thread, just so more people would see your json parser. Thank you for posting this.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1108191]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found