JSON::PC - fast JSON Parser and Converter |
JSON::PC - (DEPRECATED) fast JSON Parser and Converter
This module is too buggy and is not maintained. Please try to use the JSON::XS manpage which is faster than the JSON::Syck manpage and properly works.
Additionally, JSON module now use the JSON::XS manpage as the backend module and if not available, it uses the pure Perl module the JSON::PP manpage. Theire interfaces are incompatible to old JSON module (version 1.xx).
See to JSON.
use JSON::PC; my $json = new JSON::PC; my $obj = $json->parse(q/{foo => [1,2,3], bar => "perl"}/); print $json->convert($obj);
# or
$obj = JSON::PC::parse(q/{foo => [1,2,3], bar => "perl"}/); print JSON::PC::convert($obj);
JSON::PC is a XS version of the JSON::Parser manpage and the JSON::Converter manpage. This module supports all JSON module options.
You might want to know the difference between the JSON::Syck manpage and JSON::PC.
Since JSON::Syck is based on libsyck, JSON::Syck is supposed to be very fast and memory efficient. (from the JSON::Syck manpage doc)
JSON::PC is (in many case) faster than JSON::Syck and supports all JSON.pm options. After verion 1.90, JSON will calls JSON::PC by default.
Oh, and JSON::PC can use camelCase method names still :-(
Except new
method, all methods are object method.
new()
new(%option)
parse($str)
jsonToObj
is an alias.
convert($obj)
objToJson
is an alias.
autoconv($int)
autoconv
.
See AUTOCONVERT for more info.
skipinvalid($int)
convert()
does die()
when it encounters any invalid data
(for instance, coderefs). If skipinvalid
is set with true(integer),
the function convets these invalid data into JSON format's null
.
execcoderef($int)
convert()
does die()
when it encounters any code reference.
However, if execcoderef
is set with true(integer),
executes the coderef and uses returned value.
pretty($int)
pretty
.
When prrety is true(integer), objToJson()
returns
prrety-printed string. See PRETTY PRINTING for more info.
indent($int)
indent
.
See PRETTY PRINTING for more info.
delimiter($int)
delimiter
.
See PRETTY PRINTING for more info.
unmapping($int)
unmapping
.
See UNMAPPING OPTION for more info.
keysort($int)
keysort($code_ref)
keysort
.
See HASH KEY SORT ORDER for more info.
convblessed($int)
convblessed
.
See BLESSED OBJECT for more info.
selfconvert($int)
selfconvert
.
See BLESSED OBJECT for more info.
singlequote($int)
singlequote
.
See CONVERT WITH SINGLE QUOTES for more info.
barekey($int)
true(integer)
to parse bare keys of objects.
quotapos($int)
true(integer)
to parse
any keys and values quoted by single quotations.
utf8($int)
utf8
.
You can set a true(integer)
to set UTF8 flag into strings contain utf8.
By default, autoconv
is true.
(Perl) {num => 10.02} ( => JSON) {"num" : 10.02}
it is not {"num" : "10.02"}
.
But set 0:
(Perl) {num => 10.02} ( => JSON) {"num" : "10.02"}
it is not {"num" : 10.02}
.
If you use JSON.pm, you can explicitly sepcify:
$obj = { id => JSON::Number(10.02), bool1 => JSON::True, bool2 => JSON::False, noval => JSON::Null, };
$json->convert($obj); # {"noval" : null, "bool2" : false, "bool1" : true, "id" : 10.02}
See JSON.
By default, unMapping
is false and JSON::PC converts
null
, true
, false
into JSON::NotString
objects.
You can set true(integer)
to stop the mapping function.
In that case, JSON::PC will convert null
, true
, false
into undef
, 1, 0.
You can set a true(integer)
into barekey
for JSON::PC to parse
bare keys of objects.
$json->barekey(1); $obj = $json->parse('{foo:"bar"}');
You can set a true(integer)
for JSON::PC to parse
any keys and values quoted by single quotations.
$json->quotapos(1);
$obj = $json->parse(q|{"foo":'bar'}|); $obj = $json->parse(q|{'foo':'bar'}|);
By default convert
will serialize hashes with their keys in random
order. To control the ordering of hash keys, you can provide a standard
'sort' function that will be used to control how hashes are converted.
You can provide either a fully qualified function name or a CODEREF to $obj->keysort.
If you give any integers (excluded 0), the sort function will work as:
sub { $a cmp $b }
Note that since the sort function is external to the JSON module the magical $a and $b arguments will not be in the same package. In order to gain access to the sorting arguments, you must either:
o use the ($$) prototype (slow) o Fully qualify $a and $b from the JSON::Converter namespace
See the documentation on sort for more information.
local $JSON::KeySort = 'My::Package::sort_function';
or
local $JSON::KeySort = \&_some_function;
sub sort_function { $JSON::Converter::a cmp $JSON::Converter::b; }
or
sub sort_function ($$) { my ($a, $b) = @_;
$a cmp $b }
By default, JSON::PC doesn't deal with any blessed object
(returns undef
or null
in the JSON format).
If you use $JSON::ConvBlessed or convblessed
option,
the module can convert most blessed object (hashref or arrayref).
If you use selfconvert
option,
the module will test for a toJson()
method on the object,
and will rely on this method to obtain the converted value of
the object.
You can set a true(integer)
for JSON::PC
to set UTF8 flag into strings contain utf8.
By default true.
You can set a true(integer)
for JSON::PC
to quote any keys and values with single quotations.
You want to parse single quoted JSON data, See SINGLE QUOTATION OPTION.
These XS codes are very very dirty and should be cleaned up.
I wish to support some version less than Perl 5.8.
Should improve parse error message.
Tied variable is supported?
Test!
JSON, the JSON::Converter manpage, the JSON::Parser manpage
Makamaka Hannyaharamitu, <makamaka[at]donzoko.net>
Copyright 2006 by Makamaka Hannyaharamitu
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
JSON::PC - fast JSON Parser and Converter |