JSON - parse and convert to JSON (JavaScript Object Notation).
use JSON;
$obj = { id => ["foo", "bar", { aa => 'bb'}], hoge => 'boge' };
$js = objToJson($obj); # this is {"id":["foo","bar",{"aa":"bb"}],"hoge":"boge"}. $obj = jsonToObj($js); # the data structure was restored.
# OOP
my $json = new JSON;
$obj = {id => 'foo', method => 'echo', params => ['a','b']} $js = $json->objToJson($obj); $obj = $json->jsonToObj($js);
# pretty-printing $js = $json->objToJson($obj, {pretty => 1, indent => 2});
$json = JSON->new(pretty => 1, delimiter => 0); $json->objToJson($obj);
my $json = new JSON;
"new" can take some options.
my $json = new JSON (autoconv => 0, pretty => 1);
Following options are supported:
my $obj = [1, 2, {foo => bar}]; my $js = $json->objToJson($obj); # [1,2,{"foo":"bar"}]
By default, returned string is one-line. However, you can get pretty-printed data with "pretty" option. Please see below ``PRETY PRINTING''.
my $js = $json->objToJson($obj, {pretty => 1, indent => 2}); # [ # 1, # 2, # { # "foo" : "bar" # } # ]
(JSON) {"param" : []} ( => Perl) {'param' => []};
(JSON) {"param" : {}} ( => Perl) {'param' => {}};
(JSON) {"param" : "string"} ( => Perl) {'param' => 'string'};
JSON {"param" : null} => Perl {'param' => bless( {'value' => undef}, 'JSON::NotString' )}; or {'param' => undef}
(JSON) {"param" : true} ( => Perl) {'param' => bless( {'value' => 'true'}, 'JSON::NotString' )}; or {'param' => 1}
(JSON) {"param" : false} ( => Perl) {'param' => bless( {'value' => 'false'}, 'JSON::NotString' )}; or {'param' => 2}
(JSON) {"param" : 0xff} ( => Perl) {'param' => 255};
(JSON) {"param" : 010} ( => Perl) {'param' => 8};
These JSON::NotString objects are overloaded so you don't care about. Since 1.00, ``UnMapping option'' is added. When that option is set, {``param'' : null} will be converted into {'param' => undef}, insted of {'param' => bless( {'value' => undef}, 'JSON::NotString' )}.
Perl's "undef" is converted to 'null'.
my $str = $json->objToJson($obj, {pretty => 1, indent => 4});
In addition, you can set some number to "delimiter" option. The available numbers are only 0, 1 and 2. In pretty-printing mode, when "delimiter" is 1, one space is added after ':' in object keys. If "delimiter" is 2, it is ' : ' and 0 is ':' (default is 2). If you give 3 or more to it, the value is taken as 2.
(Perl) {num => 10.02} ( => JSON) {"num" : 10.02}
it is not "{"num" : "10.02"}".
But set false value with $JSON::AUTOCONVERT:
(Perl) {num => 10.02} ( => JSON) {"num" : "10.02"}
it is not "{"num" : 10.02}".
You can explicitly sepcify:
$obj = { id => JSON::Number(10.02), bool1 => JSON::True, bool2 => JSON::False, noval => JSON::Null, };
$json->objToJson($obj); # {"noval" : null, "bool2" : false, "bool1" : true, "id" : 10.02}
"JSON::Number()" returns "undef" when an argument invalid format.
local $JSON::BareKey = 1; $obj = jsonToObj('{foo:"bar"}');
local $JSON::QuotApos = 1; $obj = jsonToObj(q|{"foo":'bar'}|); $obj = jsonToObj(q|{'foo':'bar'}|);
With $JSON::BareKey:
local $JSON::BareKey = 1; local $JSON::QuotApos = 1; $obj = jsonToObj(q|{foo:'bar'}|);
Shall I support not only {``foo'' : ``bar''} but {foo : ``bar''} or {'foo' : 'bar'} also?
Which name is more desirable? JSONRPC or JSON::RPC.
SHIMADA pointed out many problems to me.
Mike Castle <dalgoda[at]ix.netcom.com> suggested better packaging way.
Jeremy Muhlich <jmuhlich[at]bitflood.org> help me escaped character handling in JSON::Parser.
Adam Sussman <adam.sussman[at]ticketmaster.com> suggested the octal and hexadecimal formats as number.
Tatsuhiko Miyagawa <miyagawa[at]bulknews.net> taught a terrible typo and gave some suggestions.
David Wheeler <david[at]kineticode.com> suggested me supporting pretty-printing and gave a part of ``PRETY PRINTING''.
Rusty Phillips <rphillips[at]edats.com> suggested me supporting the query object other than CGI.pm for JSONRPC::Transport::HTTP::CGI.
Felipe Gasper <gasperfm[at]uc.edu> pointed to a problem of JSON::NotString with undef. And show me patches for 'bare key option' & 'single quotation option'.
Yaman Saqqa <abulyomon[at]gmail.com> helped my decision to support the bare key option.
Alden DoRosario <adorosario[at]chitika.com> tought JSON::Conveter::_stringfy (<= 0.992) is very slow.
And Thanks very much to JSON by JSON.org (Douglas Crockford) and JSON-RPC by http://json-rpc.org/
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |