JSON - parse and convert to JSON (JavaScript Object Notation).
use JSON; $obj = { id => ["foo", "bar", { aa => 'bb'}], hoge => 'boge' }; $js = objToJson($obj); $obj = jsonToObj($js); # OOP my $json = new JSON; $js = $json->objToJson({id => 'foo', method => 'echo', params => ['a','b']}); $obj = $json->jsonToObj($js);
This module converts between JSON (JavaScript Object Notation) and Perl data structure into each other. For JSON, See to http://www.crockford.com/JSON/.
JSON {"param" : []} => Perl {'param' => []}; JSON {"param" : {}} => Perl {'param' => {}}; JSON {"param" : "string"} => Perl {'param' => 'string'}; JSON {"param" : null} => Perl {'param' => bless( {'value' => undef}, 'JSON::NotString' )}; JSON {"param" : true} => Perl {'param' => bless( {'value' => 'true'}, 'JSON::NotString' )}; JSON {"param" : false} => Perl {'param' => bless( {'value' => 'false'}, 'JSON::NotString' )}; JSON {"param" : -1.23} => Perl {'param' => bless( {'value' => '-1.23'}, 'JSON::NotString' )};
These JSON::NotString objects are overloaded so you don't care about. Perl's undef is converted to 'null'.
undef
By default $JSON::AUTOCONVERT is true.
Perl {num => 10.02} => JSON {"num" : 10.02} (not {"num" : "10.02"})
But set false value with $JSON::AUTOCONVERT:
Perl {num => 10.02} => JSON {"num" : "10.02"} (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.
JSON::Number()
new, objToJson, jsonToObj.
new
objToJson
jsonToObj
objToJson, jsonToObj.
http://www.crockford.com/JSON/ JSON::Parser JSON::Converter
Makamaka Hannyaharamitu, <makamaka[at]cpan.org>
Copyright 2005 by Makamaka Hannyaharamitu
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install JSON, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON
CPAN shell
perl -MCPAN -e shell install JSON
For more information on module installation, please visit the detailed CPAN module installation guide.