JSON::Tiny(3)

πŸ“¦
version 0.58 Β· 2 versions
, in fedora 43

πŸ’– 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::PP is configurable, but more complex. JSON::Tiny offers 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::PP and JSON::Tiny only use core dependencies. JSON::Tiny requires Perl 5.8.4, while JSON::PP requires 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::PP has 42 functions and methods. JSON::Tiny has 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::Light by 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 ✨


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