| 1 | package Cikl::Smrt::Parsers::ParseJson; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use JSON::XS; | ||
| 7 | use Mouse; | ||
| 8 | use Cikl::Smrt::Parser; | ||
| 9 | extends 'Cikl::Smrt::Parser'; | ||
| 10 | use namespace::autoclean; | ||
| 11 | |||
| 12 | use constant NAME => 'json'; | ||
| 13 | sub name { return NAME; } | ||
| 14 | |||
| 15 | has 'fields' => ( | ||
| 16 | is => 'ro', | ||
| 17 | isa => 'Str', | ||
| 18 | required => 1 | ||
| 19 | ); | ||
| 20 | |||
| 21 | has 'fields_map' => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'Str', | ||
| 24 | required => 1 | ||
| 25 | ); | ||
| 26 | |||
| 27 | sub parse { | ||
| 28 | my $self = shift; | ||
| 29 | my $fh = shift; | ||
| 30 | my $broker = shift; | ||
| 31 | |||
| 32 | my @fields = split(/\s*,\s*/,$self->fields); | ||
| 33 | my @fields_map = split(/\s*,\s*/, $self->fields_map); | ||
| 34 | my $cb = sub { | ||
| 35 | my $a = shift; | ||
| 36 | my $h = {}; | ||
| 37 | foreach (0 ... $#fields_map){ | ||
| 38 | my $v = $a->{$fields[$_]}; | ||
| 39 | if (defined($v)) { | ||
| 40 | $h->{$fields_map[$_]} = lc($v); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | $broker->emit($h); | ||
| 44 | }; | ||
| 45 | |||
| 46 | $self->_parse_json_as_stream($fh, $cb); | ||
| 47 | return(undef); | ||
| 48 | } | ||
| 49 | |||
| 50 | # This will parse the JSON as a stream, which uses significantly less memory | ||
| 51 | # than reading the entire structure in at once. | ||
| 52 | # In order to accomplish this, we have to strip off the '[' and ']' that we | ||
| 53 | # expect to surround the main JSON array in order to get the JSON lib to emit | ||
| 54 | # individual objects. We must also remove any commas that appear between | ||
| 55 | # the objects that are emitted, as the incremental parser expects distinct | ||
| 56 | # JSON objects. | ||
| 57 | # | ||
| 58 | # We're essentially going from a 'dump' json format: | ||
| 59 | # [{...},{...},{...}] | ||
| 60 | # to a 'streamed' json format: | ||
| 61 | # {...}{...}{...} | ||
| 62 | # | ||
| 63 | # The parser should also be able to handle streamed the streamed format, if | ||
| 64 | # provided. | ||
| 65 | # | ||
| 66 | sub _parse_json_as_stream { | ||
| 67 | my $self = shift; | ||
| 68 | my $fh = shift; | ||
| 69 | my $cb = shift; | ||
| 70 | |||
| 71 | my $buf; | ||
| 72 | my $json = JSON::XS->new(); | ||
| 73 | my $firstread = 1; | ||
| 74 | my $trim_comma_on_read = 0; | ||
| 75 | while (read($fh, $buf, 10000)) { | ||
| 76 | if ($firstread == 1) { | ||
| 77 | # Skip trim off the first '[', indicating the start of an array. | ||
| 78 | $buf =~ s/^\s*\[//; | ||
| 79 | $firstread = 0; | ||
| 80 | } | ||
| 81 | if (eof($fh)) { | ||
| 82 | # Strip off the trailing ']' | ||
| 83 | $buf =~ s/\]\s*$//; | ||
| 84 | } | ||
| 85 | if ($trim_comma_on_read == 1) { | ||
| 86 | $buf =~ s/^\s*,//; | ||
| 87 | $trim_comma_on_read = 0; | ||
| 88 | } | ||
| 89 | $json->incr_parse($buf); | ||
| 90 | while (my $data = $json->incr_parse()) { | ||
| 91 | $cb->($data); | ||
| 92 | if ($json->incr_text =~ s/^\s*\]\s*$//) { | ||
| 93 | # We're at the end of an object, and got the closing ']'. so, we'll | ||
| 94 | # just let ourselves finish gracefully. | ||
| 95 | last; | ||
| 96 | } | ||
| 97 | $json->incr_text =~ s/^\s*,//; | ||
| 98 | # If there's nothing left in the buffer, then iterate to the next read. | ||
| 99 | if ($json->incr_text =~ /^\s*$/) { | ||
| 100 | $trim_comma_on_read = 1; | ||
| 101 | last; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | return undef; | ||
| 107 | } | ||
| 108 | |||
| 109 | __PACKAGE__->meta->make_immutable; | ||
| 110 | |||
| 111 | 1; |