/usr/share/perl5/Mojo/JSON.pm is in libmojolicious-perl 2.98+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | package Mojo::JSON;
use Mojo::Base -base;
use B;
use Mojo::Util;
use Scalar::Util 'blessed';
has 'error';
# Literal names
my $FALSE = bless \(my $false = 0), 'Mojo::JSON::_Bool';
my $TRUE = bless \(my $true = 1), 'Mojo::JSON::_Bool';
# Escaped special character map (with u2028 and u2029)
my %ESCAPE = (
'"' => '"',
'\\' => '\\',
'/' => '/',
'b' => "\x07",
'f' => "\x0C",
'n' => "\x0A",
'r' => "\x0D",
't' => "\x09",
'u2028' => "\x{2028}",
'u2029' => "\x{2029}"
);
my %REVERSE = map { $ESCAPE{$_} => "\\$_" } keys %ESCAPE;
for (0x00 .. 0x1F, 0x7F) { $REVERSE{pack 'C', $_} //= sprintf '\u%.4X', $_ }
# Unicode encoding detection
my $UTF_PATTERNS = {
'UTF-32BE' => qr/^\0\0\0[^\0]/,
'UTF-16BE' => qr/^\0[^\0]\0[^\0]/,
'UTF-32LE' => qr/^[^\0]\0\0\0/,
'UTF-16LE' => qr/^[^\0]\0[^\0]\0/
};
my $WHITESPACE_RE = qr/[\x20\x09\x0a\x0d]*/;
# "Hey...That's not the wallet inspector..."
sub decode {
my ($self, $bytes) = @_;
# Cleanup
$self->error(undef);
# Missing input
$self->error('Missing or empty input.') and return unless $bytes;
# Remove BOM
$bytes =~ s/^(?:\357\273\277|\377\376\0\0|\0\0\376\377|\376\377|\377\376)//g;
# Wide characters
$self->error('Wide character in input.') and return
unless utf8::downgrade($bytes, 1);
# Detect and decode unicode
my $encoding = 'UTF-8';
$bytes =~ $UTF_PATTERNS->{$_} and $encoding = $_ for keys %$UTF_PATTERNS;
$bytes = Mojo::Util::decode $encoding, $bytes;
# Object or array
my $res = eval {
local $_ = $bytes;
# Leading whitespace
m/\G$WHITESPACE_RE/gc;
# Array
my $ref;
if (m/\G\[/gc) { $ref = _decode_array() }
# Object
elsif (m/\G\{/gc) { $ref = _decode_object() }
# Unexpected
else { _exception('Expected array or object') }
# Leftover data
unless (m/\G$WHITESPACE_RE\z/gc) {
my $got = ref $ref eq 'ARRAY' ? 'array' : 'object';
_exception("Unexpected data after $got");
}
$ref;
};
# Exception
if (!$res && (my $e = $@)) {
chomp $e;
$self->error($e);
}
return $res;
}
sub encode {
my ($self, $ref) = @_;
return Mojo::Util::encode 'UTF-8', _encode_values($ref);
}
sub false {$FALSE}
sub true {$TRUE}
sub _decode_array {
my @array;
until (m/\G$WHITESPACE_RE\]/gc) {
# Value
push @array, _decode_value();
# Separator
redo if m/\G$WHITESPACE_RE,/gc;
# End
last if m/\G$WHITESPACE_RE\]/gc;
# Invalid character
_exception('Expected comma or right square bracket while parsing array');
}
return \@array;
}
sub _decode_object {
my %hash;
until (m/\G$WHITESPACE_RE\}/gc) {
# Quote
m/\G$WHITESPACE_RE"/gc
or _exception('Expected string while parsing object');
# Key
my $key = _decode_string();
# Colon
m/\G$WHITESPACE_RE:/gc
or _exception('Expected colon while parsing object');
# Value
$hash{$key} = _decode_value();
# Separator
redo if m/\G$WHITESPACE_RE,/gc;
# End
last if m/\G$WHITESPACE_RE\}/gc;
# Invalid character
_exception('Expected comma or right curly bracket while parsing object');
}
return \%hash;
}
sub _decode_string {
my $pos = pos;
# Extract string with escaped characters
m#\G(((?:[^\x00-\x1F\\"]|\\(?:["\\/bfnrt]|u[A-Fa-f0-9]{4})){0,32766})*)#gc;
my $str = $1;
# Missing quote
unless (m/\G"/gc) {
_exception('Unexpected character or invalid escape while parsing string')
if m/\G[\x00-\x1F\\]/;
_exception('Unterminated string');
}
# Unescape popular characters
if (index($str, '\\u') < 0) {
$str =~ s!\\(["\\/bfnrt])!$ESCAPE{$1}!gs;
return $str;
}
# Unescape everything else
my $buffer = '';
while ($str =~ m/\G([^\\]*)\\(?:([^u])|u(.{4}))/gc) {
$buffer .= $1;
# Popular character
if ($2) { $buffer .= $ESCAPE{$2} }
# Escaped
else {
my $ord = hex $3;
# Surrogate pair
if (($ord & 0xF800) == 0xD800) {
# High surrogate
($ord & 0xFC00) == 0xD800
or pos($_) = $pos + pos($str), _exception('Missing high-surrogate');
# Low surrogate
$str =~ m/\G\\u([Dd][C-Fc-f]..)/gc
or pos($_) = $pos + pos($str), _exception('Missing low-surrogate');
# Pair
$ord = 0x10000 + ($ord - 0xD800) * 0x400 + (hex($1) - 0xDC00);
}
# Character
$buffer .= pack 'U', $ord;
}
}
# The rest
return $buffer . substr $str, pos($str), length($str);
}
# "Eternity with nerds.
# It's the Pasadena Star Trek convention all over again."
sub _decode_value {
# Leading whitespace
m/\G$WHITESPACE_RE/gc;
# String
return _decode_string() if m/\G"/gc;
# Array
return _decode_array() if m/\G\[/gc;
# Object
return _decode_object() if m/\G\{/gc;
# Number
return 0 + $1
if m/\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/gc;
# True
return $TRUE if m/\Gtrue/gc;
# False
return $FALSE if m/\Gfalse/gc;
# Null
return undef if m/\Gnull/gc;
# Invalid data
_exception('Expected string, array, object, number, boolean or null');
}
sub _encode_array {
return '[' . join(',', map { _encode_values($_) } @{shift()}) . ']';
}
sub _encode_object {
my $object = shift;
# Encode pairs
my @pairs = map { _encode_string($_) . ':' . _encode_values($object->{$_}) }
keys %$object;
# Stringify
return '{' . join(',', @pairs) . '}';
}
sub _encode_string {
my $string = shift;
# Escape string
$string =~ s!([\x00-\x1F\x7F\x{2028}\x{2029}\\"/\b\f\n\r\t])!$REVERSE{$1}!gs;
# Stringify
return "\"$string\"";
}
sub _encode_values {
my $value = shift;
# Reference
if (my $ref = ref $value) {
# Array
return _encode_array($value) if $ref eq 'ARRAY';
# Object
return _encode_object($value) if $ref eq 'HASH';
# True or false
return $value ? 'true' : 'false' if $ref eq 'Mojo::JSON::_Bool';
# Blessed reference with TO_JSON method
if (blessed $value && (my $sub = $value->can('TO_JSON'))) {
return _encode_values($value->$sub);
}
}
# Null
return 'null' unless defined $value;
# Number
my $flags = B::svref_2object(\$value)->FLAGS;
return $value
if $flags & (B::SVp_IOK | B::SVp_NOK) && !($flags & B::SVp_POK);
# String
return _encode_string($value);
}
sub _exception {
# Leading whitespace
m/\G$WHITESPACE_RE/gc;
# Context
my $context = 'Malformed JSON: ' . shift;
if (m/\G\z/gc) { $context .= ' before end of data' }
else {
my @lines = split /\n/, substr($_, 0, pos);
$context .= ' at line ' . @lines . ', offset ' . length(pop @lines || '');
}
# Throw
die "$context.\n";
}
# Emulate boolean type
package Mojo::JSON::_Bool;
use overload '0+' => sub { ${$_[0]} }, '""' => sub { ${$_[0]} }, fallback => 1;
1;
=head1 NAME
Mojo::JSON - Minimalistic JSON
=head1 SYNOPSIS
use Mojo::JSON;
my $json = Mojo::JSON->new;
my $bytes = $json->encode({foo => [1, 2], bar => 'hello!'});
my $hash = $json->decode($bytes);
=head1 DESCRIPTION
L<Mojo::JSON> is a minimalistic and relaxed implementation of RFC 4627. While
it is possibly the fastest pure-Perl JSON parser available, you should not use
it for validation.
It supports normal Perl data types like C<Scalar>, C<Array> reference, C<Hash>
reference and will try to call the C<TO_JSON> method on blessed references, or
stringify them if it doesn't exist.
[1, -2, 3] -> [1, -2, 3]
{"foo": "bar"} -> {foo => 'bar'}
Literal names will be translated to and from L<Mojo::JSON> constants or a
similar native Perl value.
true -> Mojo::JSON->true
false -> Mojo::JSON->false
null -> undef
Decoding UTF-16 (LE/BE) and UTF-32 (LE/BE) will be handled transparently,
encoding will only generate UTF-8. The two unicode whitespace characters
C<u2028> and C<u2029> will always be escaped to make JSONP easier.
=head1 ATTRIBUTES
L<Mojo::JSON> implements the following attributes.
=head2 C<error>
my $err = $json->error;
$json = $json->error('Oops!');
Parser errors.
=head1 METHODS
L<Mojo::JSON> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 C<decode>
my $array = $json->decode($bytes);
my $hash = $json->decode($bytes);
Decode JSON.
=head2 C<encode>
my $bytes = $json->encode({foo => 'bar'});
Encode Perl structure.
=head2 C<false>
my $false = Mojo::JSON->false;
my $false = $json->false;
False value, used because Perl has no native equivalent.
=head2 C<true>
my $true = Mojo::JSON->true;
my $true = $json->true;
True value, used because Perl has no native equivalent.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|