1Mojo::JSON(3)         User Contributed Perl Documentation        Mojo::JSON(3)
2
3
4

NAME

6       Mojo::JSON - Minimalistic JSON
7

SYNOPSIS

9         use Mojo::JSON qw(decode_json encode_json);
10
11         my $bytes = encode_json {foo => [1, 2], bar => 'hello!', baz => \1};
12         my $hash  = decode_json $bytes;
13

DESCRIPTION

15       Mojo::JSON is a minimalistic and possibly the fastest pure-Perl
16       implementation of RFC 8259 <http://tools.ietf.org/html/rfc8259>.
17
18       It supports normal Perl data types like scalar, array reference, hash
19       reference and will try to call the "TO_JSON" method on blessed
20       references, or stringify them if it doesn't exist. Differentiating
21       between strings and numbers in Perl is hard, depending on how it has
22       been used, a scalar can be both at the same time. The string value has
23       a higher precedence unless both representations are equivalent.
24
25         [1, -2, 3]     -> [1, -2, 3]
26         {"foo": "bar"} -> {foo => 'bar'}
27
28       Literal names will be translated to and from Mojo::JSON constants or a
29       similar native Perl value.
30
31         true  -> Mojo::JSON->true
32         false -> Mojo::JSON->false
33         null  -> undef
34
35       In addition scalar references will be used to generate booleans, based
36       on if their values are true or false.
37
38         \1 -> true
39         \0 -> false
40
41       The character "/" will always be escaped to prevent XSS attacks.
42
43         "</script>" -> "<\/script>"
44
45       For better performance the optional module Cpanel::JSON::XS (4.09+)
46       will be used automatically if possible. This can also be disabled with
47       the "MOJO_NO_JSON_XS" environment variable.
48

FUNCTIONS

50       Mojo::JSON implements the following functions, which can be imported
51       individually.
52
53   decode_json
54         my $value = decode_json $bytes;
55
56       Decode JSON to Perl value and die if decoding fails.
57
58   encode_json
59         my $bytes = encode_json {i => '♥ mojolicious'};
60
61       Encode Perl value to JSON.
62
63   false
64         my $false = false;
65
66       False value, used because Perl has no native equivalent.
67
68   from_json
69         my $value = from_json $chars;
70
71       Decode JSON text that is not "UTF-8" encoded to Perl value and die if
72       decoding fails.
73
74   j
75         my $bytes = j [1, 2, 3];
76         my $bytes = j {i => '♥ mojolicious'};
77         my $value = j $bytes;
78
79       Encode Perl data structure (which may only be an array reference or
80       hash reference) or decode JSON, an "undef" return value indicates a
81       bare "null" or that decoding failed.
82
83   to_json
84         my $chars = to_json {i => '♥ mojolicious'};
85
86       Encode Perl value to JSON text without "UTF-8" encoding it.
87
88   true
89         my $true = true;
90
91       True value, used because Perl has no native equivalent.
92

SEE ALSO

94       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
95
96
97
98perl v5.32.0                      2020-07-28                     Mojo::JSON(3)
Impressum