Skip to content

Instantly share code, notes, and snippets.

@m1el
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m1el/5cfaa59d5e6500f46323 to your computer and use it in GitHub Desktop.
Save m1el/5cfaa59d5e6500f46323 to your computer and use it in GitHub Desktop.
use JSON::XS;
use JSON::PP;
use mjson;
use Benchmark qw(:all);
my $string = "1409139024985\tINFO\t= Call duration 0 seconds";
my $json = '["1409139024985","INFO","= Call duration 0 seconds"]';
my $json2 = '{"time":"1409139024985","type":"INFO","data":"= Call duration 0 seconds\n"}';
cmpthese(100000, {
split => sub {
my @data = split /\t/, $string;
},
splithash => sub {
my @data = split /\t/, $string;
my $data = {time => $data[0], type => $data[1], data => $data[2]};
},
jsonpp => sub {
my $data = JSON::PP::decode_json($json);
},
json => sub {
my $data = JSON::XS::decode_json($json);
},
json2 => sub {
my $data = JSON::XS::decode_json($json2);
},
mjson => sub {
my $data = mjson::json($json);
}
})
#!/usr/bin/perl -w
package mjson;
use strict;
sub json {
local $_=shift;
local $^R;
m{
(?&VAL)(?{$_=$^R->[1]})
(?(DEFINE)
(?<VAL> \s*((?&STR)|(?&NUM)|(?&BOOL)|(?&OBJ)|(?&ARY))\s*)
(?<STR> ("(?:\\.|.)*?")(?{no strict 'vars';[$^R,eval$^N]}))
(?<NUM> (-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)?)(?{[$^R,eval$^N]}))
(?<BOOL> true(?{[$^R,1]})|false(?{[$^R,0]})|null(?{[$^R,undef]}))
(?<KV> \s*(?&STR)\s*:(?&VAL)(?{[$^R->[0][0],$^R->[0][1],$^R->[1]]}))
(?<OBJ> (?{[$^R,{}]}){(?:(?&KV)(?{[$^R->[0][0],{$^R->[1]=>$^R->[2]}]})(?:,(?&KV)(?{[$^R->[0][0],{%{$^R->[0][1]},$^R->[1]=>$^R->[2]}]}))*)?})
(?<ARY> (?{[$^R,[]]})\[(?:(?&VAL)(?{[$^R->[0][0],[$^R->[1]]]})(?:,(?&VAL)(?{[$^R->[0][0],[@{$^R->[0][1]},$^R->[1]]]}))*)?])
)
}xms;
$_;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment