NAME
JSON - parse and convert to JSON (JavaScript Object Notation).
SYNOPSIS
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);
DESCRIPTION
This module converts between JSON (JavaScript Object Notation) and Perl data structure into each other. For JSON, See to http://www.crockford.com/JSON/.
MAPPING
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'.
AUTOCONVERT
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.
Methods
new
, objToJson
, jsonToObj
.
EXPORT
objToJson
, jsonToObj
.
SEE ALSO
http://www.crockford.com/JSON/ JSON::Parser JSON::Converter
AUTHOR
Makamaka Hannyaharamitu, <makamaka[at]cpan.org>
COPYRIGHT AND LICENSE
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.