JSON::Tiny(3)
π JSON::Tiny π
okay so bestie, welcome to JSON::Tiny β the teeny tiny JSON module that lives in the User Contributed Perl Documentation section and has literally zero dependencies. no cap. it's small, it's fast, it's giving minimalism π
β¨ NAME β¨
JSON::Tiny β minimalistic JSON. no dependencies. that's it, that's the whole vibe π
πΈ SYNOPSIS πΈ
use JSON::Tiny qw(decode_json encode_json);
my $bytes = encode_json {foo => [1, 2], bar => 'hello!', baz => \1};
my $hash = decode_json $bytes;
like, that's the whole thing. two functions and you're done, slay β¨
π¦ DESCRIPTION π¦
JSON::Tiny is a minimalistic standalone adaptation of Mojo::JSON, straight out of the Mojolicious framework. it's a single-source-file module with under 300 lines of code and core-only dependencies. so like, one file, no CPAN spelunking, no drama π
its features include transparent Unicode support, speed, small memory footprint, and a minimal code base that's ideal for bundling or inlining. along with Mojo::JSON, it is among the fastest pure-Perl implementations of RFC 7159 <http://tools.ietf.org/html/rfc7159>. not me being fast and small, main character energy fr fr π₯
JSON::Tiny supports normal Perl data types like scalar, array reference, hash reference, and will try to call the TO_JSON method on blessed references, or stringify them if it doesn't exist. very polite of it π«Ά
differentiating between strings and numbers in 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 precedence unless both representations are equivalent. iykyk π
[1, -2, 3] -> [1, -2, 3]
{"foo": "bar"} -> {foo => 'bar'}
literal names will be translated to and from JSON::Tiny constants or a similar native Perl value:
true -> JSON::Tiny->true
false -> JSON::Tiny->false
null -> undef
scalar references will be used to generate Booleans, based on if their values are true or false:
\1 => true
\0 => false
the two Unicode whitespace characters "u2028" and "u2029" will always be escaped to make JSONP easier, and the character "/" to prevent XSS attacks. safety first bestie π‘οΈβ¨
π FUNCTIONS π
JSON::Tiny implements the following functions, which can be imported individually. pick your faves π
π decode_json
my $value = decode_json $bytes;
decode JSON to a Perl value and die if decoding fails. no soft launches here, it's giving consequences π
π encode_json
my $bytes = encode_json {foo => 'bar'};
encode a Perl value to JSON. iconic behaviour, periodt β¨
π false
my $false = false;
false value, used because Perl has no equivalent. she's filling a void π
π from_json
my $value = from_json $chars;
decode JSON text that is not "UTF-8" encoded to a Perl value, and die if decoding fails. so if your bytes are already decoded, this is your girl πΈ
π j
my $bytes = j [1, 2, 3];
my $bytes = j {foo => 'bar'};
my $value = j $bytes;
encode a Perl data structure (which may only be an array reference or hash reference) or decode JSON. an undef return value indicates a bare "null". dies if decoding fails. she's versatile, she contains multitudes π
π to_json
my $chars = to_json {i => 'β₯ Perl'};
encode a Perl value to JSON text without "UTF-8" encoding it. raw text, no filter, we love that for her π
π true
my $true = true;
true value, used because Perl has no native equivalent. queen of filling gaps π
π« More on Booleans π«
a reference to a scalar (even if blessed) is encoded as a Boolean value unless it has a TO_JSON method.
my $json = $j->encode( { b => \1, a => \0 } ); # {"b":true,"a":false}
Boolean false and true values returned when JSON is decoded are JSON::Tiny::_Bool objects with overloaded stringification. fancy π
Advanced option: users requiring a plain old literal 0 or 1, may set "$JSON::Tiny::FALSE = 0;" and "$JSON::Tiny::TRUE = 1;". any value, including blessed references will work. this must be set prior to calling a JSON decoding function. use "local" to limit scope. don't forget the local, bestie, i'm begging π
π Tiny π
JSON::Tiny compared with JSON::PP from the JSON distribution:
JSON::PPis configurable, but more complex.JSON::Tinyoffers sane defaults, and no configuration. no settings to fiddle with, we just work β¨- download and install with
"cpanm":JSON::PP, 5.2 seconds.JSON::Tiny, 1.9 seconds. we ate and left no crumbs π₯ - Minimal Dependencies: both
JSON::PPandJSON::Tinyonly use core dependencies.JSON::Tinyrequires Perl 5.8.4, whileJSON::PPrequires 5.6. - Simple Design: JSON has 2254 lines of code, six modules and five files. Distribution: 85KB.
JSON::Tiny has under 300 lines of code; an embeddable single-file module. Distribution: 18KB. tiny queen π
JSON::PPhas 42 functions and methods.JSON::Tinyhas seven. seven!! π- Performance:
Rate JSON_PP JSON_Tiny
JSON_PP 304/s -- -52%
JSON_Tiny 636/s 109% --
JSON uses JSON::XS if it's available, in which case JSON wins. see "examples/json_bench.pl" for benchmark code.
JSON::Tiny's lightweight design reduces its startup time compared to the JSON module. this may benefit frequently run applications like CGI. fast startup is such a vibe β
- Light Memory Needs: memory usage was tested with <http://valgrind.org/valgrind> and
Devel::MemoryTrace::Lightby running"examples/json_pp.pl"and"examples/json_tiny.pl".
valgrind Devel::MemoryTrace::Light
JSON::PP 5.1MB 3.7MB
JSON::Tiny 4.5MB 2.6MB
π CONFIGURATION AND ENVIRONMENT π
no configuration. nothing. zero. the absence is the feature π
πΈ DEPENDENCIES πΈ
Perl 5.8.4 or newer. Perl 5.10+ is recommended due to bugs in Perl 5.8's regular expression engine. like please upgrade bestie, for your own sake π
π INCOMPATIBILITIES π
incompatible with Exporter versions older than 5.59 (ie, predating Perl 5.8.4). ancient Exporter = no entry π
βοΈ
π AUTHOR π
David Oswald, "<davido at cpan.org>"
code and tests adapted from Mojo::JSON. credit where credit is due, always π«Ά
π¦ SUPPORT π¦
direct support requests to the author. direct bug reports to CPAN's Request Tracker (RT).
you can find documentation for this module with the perldoc command.
perldoc JSON::Tiny
you may look for additional information at:
- Github: development is hosted on Github at:
<http://www.github.com/daoswald/JSON-Tiny>
- RT: CPAN's request tracker (bug reports)
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=JSON-Tiny>
- AnnoCPAN: annotated CPAN documentation
<http://annocpan.org/dist/JSON-Tiny>
- CPAN Ratings
<http://cpanratings.perl.org/d/JSON-Tiny>
- Search CPAN
<http://search.cpan.org/dist/JSON-Tiny/>
β ACKNOWLEDGEMENTS β
Mojolicious team for its lightweight JSON implementation. this module was adapted from Mojo::JSON because it is robust, minimal, and well tested. Mojo::JSON's tests were also adapted to a dependency-free design. standing on the shoulders of legends π«Ά
Christian Hansen, whos GitHub Gist <https://gist.github.com/chansen/810296> formed the basis for Mojo::JSON, and subsequently JSON::Tiny. (yes, "whos" β we're quoting, we don't fix, that's the rule π
)
Randal Schwartz showed his pure-regexp JSON parser (PerlMonks <http://perlmonks.org/?node_id=995856>) to Los Angeles Perl Mongers (09/2012). he wasn't involved in JSON::Tiny, but exploring alternatives to his solution led to this project. accidental inspiration is still inspiration β¨
π LICENSE AND COPYRIGHT π
Copyright 2012-2014 David Oswald.
this program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
see <http://www.perlfoundation.org/artistic_license_2_0> for more information.
π SEE ALSO π
Mojo::JSON, JSON, RFC7159 <http://tools.ietf.org/html/rfc7159>.
ok that's the whole page bestie, go forth and encode responsibly πβ¨
this lil tour was brought to you by perl v5.42.0, dated 2025-07-25 β and yeah, that's the JSON::Tiny(3) manpage, rendered with love, glitter, and a truly unreasonable number of emoji ππ¦π
π¦ where this page comes from
- fedora 43: perl-JSON-Tiny 0.58-25.fc43 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms