NAME

Text::JSON::Nibble - Nibble complete JSON objects from buffers

VERSION

Version 0.05

SYNOPSIS

Example1 (Basic usage)

The basic usage of the module, Give it a stream of JSON in text form, it will extract the first complete block of JSON it finds.

use warnings;
use strict;

use Text::JSON::Nibble;
use JSON::MaybeXS;

my $json = JSON->new;
my $item = Text::JSON::Nibble->new();

my $test = {
		lol => {
				a => [1,2,3],
				b => "lol"
		}
};

my $jsontext = $json->encode($test);

$jsontext = "$jsontext$jsontext";

print "jsontext: $jsontext\n";

my ($text,$offset) = $item->digest($jsontext);

print "Text: $text\n";
print "Offset: $offset\n";

# Beware the offset is a real offset so the first character is classed as 0, the literal length is 1 greater;
$jsontext = substr($jsontext,$offset+1);

print "new jsontext: $jsontext\n";

Example2 (Streaming usage)

This is a more efficient version for dealing with sockets that can be connected for a long period, NOTE this is not a copy/paste example, please read carefully.

use warnings;
use strict;

use Text::JSON::Nibble;
use POE,ASYNC,...

sub handler_for_when_socket_connected {
	$mystash->{nibbler} = Text::JSON::Nibble->new();
}

sub handler_tor_data_recvd {
	$mystash->{nibbler}->load($information_recvd);

	# queue will return 0 when it has nothing
	while ($mystash->{nibbler}->queue) {
		# Grab the first item from the queue
		my $jsonChunk = $mystash->{nibbler}->pull;
		
		# Do something with it
		$superCommand->magic($jsonChunk);
	}
}

WARNING

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 streamnig socket buffer.

Use this only when needed.

SUBROUTINES/METHODS

new

Generate a new JSON Nibble object

digest

Digest the text that is fed and attempt to return a complete JSON object from it, returns two items the JSON object (in text form) and the offset in the buffer.

On a failure it will return "" and 0

load

Load information into the buffer for processing

queue

Return how many objects are in the queue

pull

Pull the first object from the queue

clear

Clear the internal state and buffer state of nibble, like a brand new() object.

_proc

Proccess text into json (Do not call this directly)

AUTHOR

Paul G Webster, <daemon at cpan.org>

BUGS

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.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Text::JSON::Nibble

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2017 Paul G Webster.

This program is released under the following license: BSD