NAME
JSON::YAJL::Generator - JSON generation with YAJL
SYNOPSIS
use JSON::YAJL;
my $generator = JSON::YAJL::Generator->new();
# or to beautify (indent):
# my $generator = JSON::YAJL::Generator->new( 1, ' ' );
$generator->map_open();
$generator->string("integer");
$generator->integer(123);
$generator->string("double");
$generator->double("1.23");
$generator->string("number");
$generator->number("3.141");
$generator->string("string");
$generator->string("a string");
$generator->string("string2");
$generator->string("another string");
$generator->string("null");
$generator->null();
$generator->string("true");
$generator->bool(1);
$generator->string("false");
$generator->bool(0);
$generator->string("map");
$generator->map_open();
$generator->string("key");
$generator->string("value");
$generator->string("array");
$generator->array_open();
$generator->integer(1);
$generator->integer(2);
$generator->integer(3);
$generator->array_close();
$generator->map_close();
$generator->map_close();
print $generator->get_buf;
$generator->clear;
# This prints non-beautified:
{"integer":123,"double":1.2299999999999999822,"number":3.141,"string":"a string","string2":"another string","null":null,"true":true,"false":false,"map":{"key":"value","array":[1,2,3]}}
# or beautified:
{
"integer": 123,
"double": 1.2299999999999999822,
"number": 3.141,
"string": "a string",
"string2": "another string",
"null": null,
"true": true,
"false": false,
"map": {
"key": "value",
"array": [
1,
2,
3
]
}
}
DESCRIPTION
This module allows you to generate JSON with YAJL. This is quite a low-level interface for generating JSON and it accumulates JSON in an internal buffer until you fetch it.
If you create certain invalid JSON constructs then this module throws an exception.
This is a very early release to see how cross-platform the underlying code is. The API may change in future.
METHODS
new
The constructor. You can pass in a flag on whether to generate indented (beautiful) output and if so how many spaces to use:
my $generator = JSON::YAJL::Generator->new();
# or to beautify (indent):
# my $generator = JSON::YAJL::Generator->new( 1, ' ' );
null
Generate a null:
$generator->null();
bool
Generate a boolean value:
$generator->bool(1);
$generator->bool(0);
integer
Generate an integer:
$generator->integer(123);
double
Generate a floating point number:
$generator->double("1.23");
number
Generate a generic number. The underlying C library has to specify numeric types, but in Perl this is unnecessary so you probably just want to use this method:
$generator->number("3.141");
string
Generate a string:
$generator->string("a string");
map_open
Begins a hash:
$generator->map_open();
map_close
Ends a hash:
$generator->map_close();
array_open
Begins an array:
$generator->array_open();
array_close
Ends an array:
$generator->array_close();
get_buf
Access the null terminated generator buffer. If incrementally outputing JSON, one should call clear to clear the buffer. This allows stream generation.
print $generator->get_buf;
clear
Clear the output buffer, but maintain all internal generation state. This function will not "reset" the generator state, and is intended to enable incremental JSON outputing:
$generator->clear;
AUTHOR
Leon Brocard <acme@astray.com>
LICENSE
This module is free software; you can redistribute it or modify it under the same terms as Perl itself.