TABLE OF CONTENTS

NAME

Mojo::JSON - 简约的 JSON 处理

SYNOPSIS

use Mojo::JSON qw(decode_json encode_json);

my $bytes = encode_json {foo => [1, 2], bar => 'hello!', baz => \1};
my $hash  = decode_json $bytes;

DESCRIPTION

Mojo::JSON 是一个简约的并且有可能是最快的纯 Perl 实现的 RFC 7159.

它支持标准的 Perl 的数据结构类型, 象标量, 数组引用, 哈希引用, 并会尝试使用 TO_JSON 的方法来绑定这个引用.

在 Perl 中字符串和数字的差异很难区分, 这取决于它是如何使用, 一个标量可能二种可能都是存在的, 这个中字符串的有更高的优先级. 除非它是等效的.

[1, -2, 3]     -> [1, -2, 3]
{"foo": "bar"} -> {foo => 'bar'}

一些常量名字会被 Mojo::JSON 常量或者本地类似的值转换.

true  -> Mojo::JSON->true
false -> Mojo::JSON->false
null  -> undef

象标量的引用, 如果他们的值是真或者假, 会被当成布尔型.

\1 -> true
\0 -> false

有二个 Unicode 的空白字符 u2028u2029 总是会转义, 这样会让 JSONP 更加容易, 还有会有字符 / , 以防 xss 攻击.

"\x{2028}\x{2029}</script>" -> "\u2028\u2029<\/script>"

FUNCTIONS

Mojo::JSON implements the following functions, which can be imported individually.

decode_json

my $value = decode_json $bytes;

Decode JSON to Perl value and die if decoding fails.

encode_json

my $bytes = encode_json {i => '♥ mojolicious'};

Encode Perl value to JSON.

false

my $false = false;

False value, used because Perl has no native equivalent.

from_json

my $value = from_json $chars;

Decode JSON text that is not UTF-8 encoded to Perl value and die if decoding fails.

j

my $bytes = j [1, 2, 3];
my $bytes = j {i => '♥ mojolicious'};
my $value = j $bytes;

Encode Perl data structure (which may only be an array reference or hash reference) or decode JSON, an undef return value indicates a bare null or that decoding failed.

to_json

my $chars = to_json {i => '♥ mojolicious'};

Encode Perl value to JSON text without UTF-8 encoding it.

true

my $true = true;

True value, used because Perl has no native equivalent.

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.