Hi. I'm looking for a simple way to parse JSON objects out of a stream.
I am calling an HTTP service that returns a stream of json objects over time.
To read it, i registered a simple callback handler in LWP::UserAgent
$ua->add_handler( response_data => sub { my($response, $ua, $h, $data) = @_; return "true" if $data =~ /^\s*$/; print "Callback triggered\n"; print "data: .${data}.\n"; my $result = eval { $json->decode( $data ) }; if ($@){ print "error: $@\n"; return "true"; }; print Dumper $result; return "true"; } );
As expected, i sometimes get partial json objects in $data.
My first idea would be to so something like m/{.*?}/ against $data. Or rather some more elaborate version that deals with balanced parens and quoted strings.
My question is, is there an easier / simpler / more straightforward way to deal with something like this that I am missing?
-- SecEDIT: to clarify a bit.
The problem is that in the callback $data may just be
which means I have to grab one object, handle it, keep the rest and wait for more data.{ "foo": "bar" } { "foo" : "q
I know how to write code for that "the long way". I was asking if there is something clever or an existing module that can help with this.
In reply to Parsing JSON out of an incremental stream by Sec
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |