Deprecated.
NAME
JSON::PC - (DEPRECATED) fast JSON Parser and Converter
DEPRECATED
This module is too buggy and is not maintained. Please try to use JSON::XS which is faster than JSON::Syck and properly works.
Additionally, JSON module now use JSON::XS as the backend module and if not available, it uses the pure Perl module JSON::PP. Theire interfaces are incompatible to old JSON module (version 1.xx).
See to JSON.
SYNOPSIS
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);
DESCRIPTION
JSON::PC is a XS version of JSON::Parser and JSON::Converter. This module supports all JSON module options.
DIFFERENCE WITH JSON::Syck
You might want to know the difference between JSON::Syck and JSON::PC.
Since JSON::Syck is based on libsyck, JSON::Syck is supposed to be very fast and memory efficient. (from JSON::Syck 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 :-(
METHODS
Except new
method, all methods are object method.
- new()
- new(%option)
-
This is a class method and returns new JSON::PC object.
- parse($str)
- parse($str, $options_ref)
-
takes JSON foramt string and returns perl data structure.
jsonToObj
is an alias. - convert($obj)
- convert($obj, $options_ref)
-
takes perl data structure and returns JSON foramt string.
objToJson
is an alias. - autoconv($int)
-
This is an accessor to
autoconv
. See "AUTOCONVERT" for more info. - skipinvalid($int)
-
convert()
doesdie()
when it encounters any invalid data (for instance, coderefs). Ifskipinvalid
is set with true(integer), the function convets these invalid data into JSON format'snull
. - execcoderef($int)
-
convert()
doesdie()
when it encounters any code reference. However, ifexeccoderef
is set with true(integer), executes the coderef and uses returned value. - pretty($int)
-
This is an accessor to
pretty
. When prrety is true(integer),objToJson()
returns prrety-printed string. See "PRETTY PRINTING" for more info. - indent($int)
-
This is an accessor to
indent
. See "PRETTY PRINTING" for more info. - delimiter($int)
-
This is an accessor to
delimiter
. See "PRETTY PRINTING" for more info. - unmapping($int)
-
This is an accessor to
unmapping
. See "UNMAPPING OPTION" for more info. - keysort($int)
- keysort($code_ref)
-
This is an accessor to
keysort
. See "HASH KEY SORT ORDER" for more info. - convblessed($int)
-
This is an accessor to
convblessed
. See "BLESSED OBJECT" for more info. - selfconvert($int)
-
This is an accessor to
selfconvert
. See "BLESSED OBJECT" for more info. - singlequote($int)
-
This is an accessor to
singlequote
. See "CONVERT WITH SINGLE QUOTES" for more info. - barekey($int)
-
You can set a true(integer) to parse bare keys of objects.
- quotapos($int)
-
You can set a true(integer) to parse any keys and values quoted by single quotations.
- utf8($int)
-
This is an accessor to
utf8
. You can set a true(integer) to set UTF8 flag into strings contain utf8.
OPTIONS
AUTOCONVERT
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.
UNMAPPING OPTION
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.
BARE KEY OPTION
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"}');
SINGLE QUOTATION OPTION
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'}|);
HASH KEY SORT ORDER
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
}
BLESSED OBJECT
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.
UTF8
You can set a true(integer) for JSON::PC to set UTF8 flag into strings contain utf8. By default true.
CONVERT WITH SINGLE QUOTES
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".
TODO
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!
SEE ALSO
JSON, JSON::Converter, JSON::Parser
AUTHOR
Makamaka Hannyaharamitu, <makamaka[at]donzoko.net>
COPYRIGHT AND LICENSE
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.