| 1 | package Mojo::JSON; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 41 | ▶ | use Mojo::Base -strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 41 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 41 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 41 | ▶ | use JSON::PP (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 41 | ▶ | use Mojo::Util qw(decode encode monkey_patch); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 41 | ▶ | use overload (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 41 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # For better performance Cpanel::JSON::XS is required | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 41 | ▶ | use constant JSON_XS => $ENV{MOJO_NO_JSON_XS} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | ? 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 41 | ▶ | : !!eval { require Cpanel::JSON::XS; Cpanel::JSON::XS->VERSION('4.20'); 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 41 | ▶ | use constant CORE_BOOLS => defined &builtin::is_bool; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # Maximum nesting level for decoding, to match the default of Cpanel::JSON::XS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 41 | ▶ | use constant MAX_DEPTH => 512; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 41 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 41 | warnings->unimport('experimental::builtin') if CORE_BOOLS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # Deep recursion is expected when working with nested data structures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 41 | ▶ | no warnings 'recursion'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 41 | our @EXPORT_OK = qw(decode_json encode_json false from_json j to_json true); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # Current nesting level while decoding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 41 | our $DEPTH = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # Escaped special character map | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 41 | my %ESCAPE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | = ('"' => '"', '\\' => '\\', '/' => '/', 'b' => "\x08", 'f' => "\x0c", 'n' => "\x0a", 'r' => "\x0d", 't' => "\x09"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 41 | my %REVERSE = map { $ESCAPE{$_} => "\\$_" } keys %ESCAPE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 41 | ▶ | for (0x00 .. 0x1f) { $REVERSE{pack 'C', $_} //= sprintf '\u%.4X', $_ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$REVERSE{pack "C", $_} //= sprintf("\\u%.4X", $_)
MC/DC100%1/1
$REVERSE{pack "C", $_} //= sprintf("\\u%.4X", $_)
$REVERSE{pack "C", $_}
Truth table100%2/2
A
A= $REVERSE{pack "C", $_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # Replace pure-Perl fallbacks if Cpanel::JSON::XS is available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 41 | if (JSON_XS) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 41 | my $BINARY = Cpanel::JSON::XS->new->utf8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 40 | my $TEXT = Cpanel::JSON::XS->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | $_->canonical->allow_nonref->allow_unknown->allow_blessed->convert_blessed->stringify_infnan->escape_slash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ->allow_dupkeys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 40 | for $BINARY, $TEXT; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 6 | ▶ | monkey_patch __PACKAGE__, 'encode_json', sub { $BINARY->encode($_[0]) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | monkey_patch __PACKAGE__, 'decode_json', sub { $BINARY->decode($_[0]) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Subroutine
decode_json: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | monkey_patch __PACKAGE__, 'to_json', sub { $TEXT->encode($_[0]) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_json: not called CC=1 SCAR=0.4
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | monkey_patch __PACKAGE__, 'from_json', sub { $TEXT->decode($_[0]) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_json: not called CC=1 SCAR=0.4
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ▶ | sub decode_json { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | my $err = _decode(\my $value, shift); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ▶ | return defined $err ? croak $err : $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | ▶ | sub encode_json { encode('UTF-8', _encode_value(shift)) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 14 | ▶ | sub false () {JSON::PP::false} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
false: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | sub from_json { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | my $err = _decode(\my $value, shift, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | return defined $err ? croak $err : $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 16 | ▶ | sub j { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
j: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 16 | ▶ | return encode_json($_[0]) if ref $_[0] eq 'ARRAY' || ref $_[0] eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[0] eq "ARRAY" or ref $_[0] eq "HASH"
MC/DC50%1/2
ref $_[0] eq "ARRAY" or ref $_[0] eq "HASH"
ref $_[0] eq "ARRAY"
ref $_[0] eq "HASH"
Truth table66%2/3
A or B
A= ref $_[0] eq "ARRAY"B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 14 | return scalar eval { decode_json($_[0]) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | sub to_json { _encode_value(shift) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 20 | ▶ | sub true () {JSON::PP::true} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
true: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 77 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 77 | my $valueref = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 77 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # Missing input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 77 | ▶ | die "Missing or empty input at offset 0\n" unless length(local $_ = shift); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 76 | ▶ | $_ = decode('UTF-8', $_) unless shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 76 | ▶ | die "Input is not UTF-8 encoded\n" unless defined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # Value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 74 | $$valueref = _decode_value(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Leftover data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 58 | ▶ | /\G[\x20\x09\x0a\x0d]*\z/gc or _throw('Unexpected data'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
/\G[\x20\x09\x0a\x0d]*\z/cgu || _throw('Unexpected data')
MC/DC0%0/2
/\G[\x20\x09\x0a\x0d]*\z/cgu || _throw('Unexpected data')
/\G[\x20\x09\x0a\x0d]*\z/cgu
_throw('Unexpected data')
Truth table33%1/3
A || B
A= /\G[\x20\x09\x0a\x0d]*\z/cguB= _throw('Unexpected data')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } ? return undef : chomp $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 24 | return $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1847 | ▶ | sub _decode_array { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_array: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # Nesting limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1847 | ▶ | _throw('Nesting too deep') if (local $DEPTH = $DEPTH + 1) > MAX_DEPTH; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1845 | my @array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1845 | ▶ | until (m/\G[\x20\x09\x0a\x0d]*\]/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # Value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 1865 | push @array, _decode_value(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # Separator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1092 | ▶ | redo if /\G[\x20\x09\x0a\x0d]*,/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # End | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 1066 | ▶ | last if /\G[\x20\x09\x0a\x0d]*\]/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # Invalid character | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | _throw('Expected comma or right square bracket while parsing array'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 1070 | return \@array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 1297 | ▶ | sub _decode_object { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_object: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # Nesting limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1297 | ▶ | _throw('Nesting too deep') if (local $DEPTH = $DEPTH + 1) > MAX_DEPTH; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 1296 | my %hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1296 | ▶ | until (m/\G[\x20\x09\x0a\x0d]*\}/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # Quote | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 1300 | ▶ | /\G[\x20\x09\x0a\x0d]*"/gc or _throw('Expected string while parsing object'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # Key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 1297 | my $key = _decode_string(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # Colon | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1297 | ▶ | /\G[\x20\x09\x0a\x0d]*:/gc or _throw('Expected colon while parsing object'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # Value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 1296 | $hash{$key} = _decode_value(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # Separator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 528 | ▶ | redo if /\G[\x20\x09\x0a\x0d]*,/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # End | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 522 | ▶ | last if /\G[\x20\x09\x0a\x0d]*\}/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # Invalid character | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | _throw('Expected comma or right curly bracket while parsing object'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 523 | return \%hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 1344 | ▶ | sub _decode_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_string: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 1344 | my $pos = pos; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # Extract string with escaped characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 1344 | m!\G((?:(?:[^\x00-\x1f\\"]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})){0,32766})*)!gc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1344 | my $str = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # Invalid character | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1344 | ▶ | unless (m/\G"/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | _throw('Unexpected character or invalid escape while parsing string') if /\G[\x00-\x1f\\]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | _throw('Unterminated string'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # Unescape popular characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 1342 | ▶ | if (index($str, '\\u') < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 1337 | $str =~ s!\\(["\\/bfnrt])!$ESCAPE{$1}!gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 1337 | return $str; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # Unescape everything else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 5 | my $buffer = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 5 | ▶ | while ($str =~ /\G([^\\]*)\\(?:([^u])|u(.{4}))/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 10 | $buffer .= $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # Popular character | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 10 | ▶ | if ($2) { $buffer .= $ESCAPE{$2} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # Escaped | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 6 | my $ord = hex $3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # Surrogate pair | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 6 | ▶ | if (($ord & 0xf800) == 0xd800) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # High surrogate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 3 | ▶ | ($ord & 0xfc00) == 0xd800 or pos = $pos + pos($str), _throw('Missing high-surrogate'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # Low surrogate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | ▶ | $str =~ /\G\\u([Dd][C-Fc-f]..)/gc or pos = $pos + pos($str), _throw('Missing low-surrogate'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 1 | $ord = 0x10000 + ($ord - 0xd800) * 0x400 + (hex($1) - 0xdc00); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # Character | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 4 | $buffer .= pack 'U', $ord; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # The rest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 3 | return $buffer . substr $str, pos($str), length($str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 3235 | ▶ | sub _decode_value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_value: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # Leading whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 3235 | /\G[\x20\x09\x0a\x0d]*/gc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # String | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 3235 | ▶ | return _decode_string() if /\G"/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # Object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 3188 | ▶ | return _decode_object() if /\G\{/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # Array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 1891 | ▶ | return _decode_array() if /\G\[/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # Number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 44 | ▶ | return 0 + $1 if /\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # True | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 16 | ▶ | return true() if /\Gtrue/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 11 | ▶ | return false() if /\Gfalse/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # Null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 7 | ▶ | return undef if /\Gnull/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # Invalid character | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 2 | _throw('Expected string, array, object, number, boolean or null'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 52 | ▶ | sub _encode_array { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_array: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 52 | '[' . join(',', map { _encode_value($_) } @{$_[0]}) . ']'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 19 | ▶ | sub _encode_object { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_object: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 19 | my $object = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 19 | my @pairs = map { _encode_string($_) . ':' . _encode_value($object->{$_}) } sort keys %$object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 19 | return '{' . join(',', @pairs) . '}'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 57 | ▶ | sub _encode_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 57 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 57 | $str =~ s!([\x00-\x1f\\"/])!$REVERSE{$1}!gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 57 | return "\"$str\""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 155 | ▶ | sub _encode_value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_value: called CC=18 SCAR=28.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 155 | my $value = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # Reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 155 | ▶ | if (my $ref = ref $value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | # Object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 88 | ▶ | return _encode_object($value) if $ref eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # Array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 69 | ▶ | return _encode_array($value) if $ref eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # True or false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 17 | ▶ | return $$value ? 'true' : 'false' if $ref eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 15 | ▶ | return $value ? 'true' : 'false' if $ref eq 'JSON::PP::Boolean'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # Everything else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 6 | ▶ | return 'null' unless blessed $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 5 | ▶ | return overload::Method($value, '""') ? _encode_string($value) : 'null' unless my $sub = $value->can('TO_JSON'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 2 | return _encode_value($value->$sub); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # Null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 67 | ▶ | return 'null' unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # Boolean | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 63 | ▶ | return $value ? 'true' : 'false' if CORE_BOOLS && builtin::is_bool($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # Number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 41 | ▶ | no warnings 'numeric'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 61 | ▶ | return $value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
not utf8::is_utf8($value) and length((my $dummy = "") & $value) and 0 + $value eq $value and $value * 0 == 0
not utf8::is_utf8($value) and length((my $dummy = "") & $value) and 0 + $value eq $value
not utf8::is_utf8($value) and length((my $dummy = "") & $value)
MC/DC100%4/4
not utf8::is_utf8($value) and length((my $dummy = "") & $value) and 0 + $value eq $value and $value * 0 == 0
not utf8::is_utf8($value)
length((my $dummy = "") & $value)
0 + $value eq $value
$value * 0 == 0
Truth table100%5/5
not(A) and B and C and D
A= not utf8::is_utf8($value)B= length((my $dummy = "") & $value)C= 0 + $value eq $valueD= $value * 0 == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | if !utf8::is_utf8($value) && length((my $dummy = '') & $value) && 0 + $value eq $value && $value * 0 == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # String | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 38 | return _encode_string($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 21 | ▶ | sub _throw { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_throw: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | # Leading whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 21 | /\G[\x20\x09\x0a\x0d]*/gc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # Context | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 21 | my $context = 'Malformed JSON: ' . shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | if (m/\G\z/gc) { $context .= ' before end of data' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 21 | my @lines = split /\n/, substr($_, 0, pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 21 | ▶ | $context .= ' at line ' . @lines . ', offset ' . length(pop @lines || ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
pop @lines || ''
MC/DC100%1/1
pop @lines || ''
pop @lines
Truth table100%2/2
A
A= pop @lines
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 21 | die "$context\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | Mojo::JSON - Minimalistic JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | use Mojo::JSON qw(decode_json encode_json); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | my $bytes = encode_json {foo => [1, 2], bar => 'hello!', baz => \1}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | my $hash = decode_json $bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | L<Mojo::JSON> is a minimalistic and possibly the fastest pure-Perl implementation of L<RFC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 8259|https://tools.ietf.org/html/rfc8259>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | It supports normal Perl data types like scalar, array reference, hash reference and will try to call the C<TO_JSON> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | method on blessed references, or stringify them if it doesn't exist. Differentiating between strings and numbers in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | Perl is hard, depending on how it has been used, a scalar can be both at the same time. The string value has a higher | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | precedence unless both representations are equivalent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | [1, -2, 3] -> [1, -2, 3] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | {"foo": "bar"} -> {foo => 'bar'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | Literal names will be translated to and from L<Mojo::JSON> constants or a similar native Perl value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | true -> Mojo::JSON->true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | false -> Mojo::JSON->false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | null -> undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | In addition scalar references will be used to generate booleans, based on if their values are true or false. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | \1 -> true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | \0 -> false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | The character C</> will always be escaped to prevent XSS attacks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | "</script>" -> "<\/script>" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | For better performance the optional module L<Cpanel::JSON::XS> (4.20+) will be used automatically if possible. This can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | also be disabled with the C<MOJO_NO_JSON_XS> environment variable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | L<Mojo::JSON> implements the following functions, which can be imported individually. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | =head2 decode_json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | my $value = decode_json $bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | Decode JSON to Perl value and die if decoding fails. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | =head2 encode_json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | my $bytes = encode_json {i => '♥ mojolicious'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | Encode Perl value to JSON. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | =head2 false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | my $false = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | False value, used because Perl has no native equivalent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | =head2 from_json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | my $value = from_json $chars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | Decode JSON text that is not C<UTF-8> encoded to Perl value and die if decoding fails. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | =head2 j | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | my $bytes = j [1, 2, 3]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | my $bytes = j {i => '♥ mojolicious'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | my $value = j $bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | Encode Perl data structure (which may only be an array reference or hash reference) or decode JSON, an C<undef> return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | value indicates a bare C<null> or that decoding failed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | =head2 to_json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | my $chars = to_json {i => '♥ mojolicious'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | Encode Perl value to JSON text without C<UTF-8> encoding it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | =head2 true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | my $true = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | True value, used because Perl has no native equivalent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||