Text::JSON::Nibble - Nibble complete JSON objects from buffers
Version 1.01
This module should be used with caution, it will not handle 'badly formed' json well, its entire purpose was because I was experiencing segfaults with Cpanel::XS's decode_prefix when dealing with a streaming socket buffer.
This module is a 'character' crawling JSON extractor for plain TEXT, usable in both a 'streaming' or 'block' method, for when you need something that is not XS.
It is particularly handy for when you want to deal with JSON without decoding it.
use Text::JSON::Nibble; my $json = '{"lol":{"a":[1,2,3],"b":"lol"}}'; my $item = Text::JSON::Nibble->new(); my @results = @{ $item->digest($json) };
use Text::JSON::Nibble; my $json = '{"lol":{"a":[1,2,3],"b":"lol"}}{"lol":{"a":[1,2,3],"b":"lol"}}'; my $item = Text::JSON::Nibble->new(); foreach my $jsonBlock ( @{ $item->digest($json) } ) { print "Found: $jsonBlock\n"; } # Will display the following: # Found: {"lol":{"a":[1,2,3],"b":"lol"}} # Found: {"lol":{"a":[1,2,3],"b":"lol"}}
use Text::JSON::Nibble; my $json = '\cxa4GL<A{"lol":{"a":[1,2,3],"b":"lol"}}He Random Stuf${"lol":{"a":[1,2,3],"b":"lol"}}\cxa4GL<A'; my $item = Text::JSON::Nibble->new(); foreach my $jsonBlock ( @{ $item->digest($json) } ) { print "Found: $jsonBlock\n"; } # Will display the following: # Found: {"lol":{"a":[1,2,3],"b":"lol"}} # Found: {"lol":{"a":[1,2,3],"b":"lol"}}
use Text::JSON::Nibble; my @jsonStream = qw( {"test":1} {"moreTest":2} {"part ial":3} ); my $item = Text::JSON::Nibble->new(); $item->process( shift @jsonStream ); while( $item->stack ) { my $jsonBlock = $item->pull; print "Found $jsonBlock\n"; while ( my $newJSON = shift @jsonStream ) { $item->process($newJSON); } }
Generate a new JSON Nibble object
Digest the text that is fed in and attempt to return a complete an array of JSON object from it, returns either a blank array or an array of text-encoded-json.
Note you can call and use this at any time, even if you are using streaming functionality.
Load data into the buffer for json extraction, can be called at any point.
This function will return the buffer length remaining after extraction has been attempted.
This function takes 1 optional argument, text to be added to the buffer.
Return the amount of succesfully extracted JSON blocks ready to be pulled.
If no JSON blocks are ready, returns 0.
This function takes no arguments.
Pull an item from the stack, shortening the stack by 1.
This function will return "" if the stack is empty.
Effectively flushs the objects buffers, giving you a clean object, this can be handy when you want to start processing from another stream.
This function returns nothing.
Paul G Webster, <daemon at cpan.org>
<daemon at cpan.org>
Please report any bugs or feature requests to bug-text-json-nibble at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-JSON-Nibble. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-text-json-nibble at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Text::JSON::Nibble
You can also look for information at:
The author publishs this module to GitLab (Please report bugs here)
https://gitlab.com/paul-g-webster/PL-Text-JSON-Nibble
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-JSON-Nibble
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Text-JSON-Nibble
CPAN Ratings
http://cpanratings.perl.org/d/Text-JSON-Nibble
Search CPAN
http://search.cpan.org/dist/Text-JSON-Nibble/
Copyright 2017 Paul G Webster.
This program is released under the following license: BSD
To install Text::JSON::Nibble, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::JSON::Nibble
CPAN shell
perl -MCPAN -e shell install Text::JSON::Nibble
For more information on module installation, please visit the detailed CPAN module installation guide.