=pod

=encoding utf8

=head1 NAME

JSON::Tiny - Minimalistic JSON. No dependencies.

=head1 SYNOPSIS

    use JSON::Tiny qw(decode_json encode_json);

    # Encode and decode JSON (die on errors)
    my $bytes = encode_json({foo => [1, 2], bar => 'hello!', baz => \1});
    my $hash  = decode_json($bytes);

    # Handle errors
    my $json = JSON::Tiny->new;
    if(defined(my $hash = $json->decode($bytes))) { say $hash->{message} }
    else { say 'Error: ', $json->error }

    # Use the shortcut interface (die on errors)
    use JSON::Tiny 'j';
    my $bytes = j({foo => [1, 2], bar => 'hello!', baz => \1});
    my $hash  = j($bytes);

=head1 DESCRIPTION

L<JSON::Tiny> is a minimalistic standalone adaptation of L<Mojo::JSON>, from
the L<Mojolicious> framework. It is a single-source-file module with 350 lines
of code and core-only dependencies.

Key features include L<RFC 4627|http://tools.ietf.org/search/rfc4627> JSON, 
transparent Unicode support, speed, small memory footprint, and a minimal 
code base ideal for bundling or inlining. L<JSON::Tiny> and Mojo::JSON are 
possibly the fastest pure-Perl implementations of 
L<RFC 4627|http://tools.ietf.org/search/rfc4627>.

L<JSON::Tiny> 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<JSON::Tiny> constants or a
similar native Perl value. In addition C<Scalar> references will be used to
generate booleans, based on if their values are true or false.

    true  -> JSON::Tiny->true
    false -> JSON::Tiny->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 FUNCTIONS

L<JSON::Tiny> implements the following functions, which can be imported
individually.

=head2 decode_json

  my $array = decode_json($bytes);
  my $hash  = decode_json($bytes);

Decode JSON to Perl data structure and die if decoding fails.

=head2 encode_json

  my $bytes = encode_json([1, 2, 3]);
  my $bytes = encode_json({foo => 'bar'});

Encode Perl data structure to JSON.

=head2 j

    my $bytes = j([1, 2, 3]);
    my $bytes = j({foo => 'bar'});
    my $array = j($bytes);
    my $hash  = j($bytes);

Encode Perl data structure or decode JSON and return C<undef> if decoding
fails.

Dies with a C<JSON::Tiny::error> message on decode failure.

=head1 ATTRIBUTES

L<JSON::Tiny> implements the following attributes.

=head2 error

    my $err = $json->error;
    $json   = $json->error('Parser error');

Parser errors.

=head1 METHODS

L<JSON::Tiny> implements the following methods.

=head2 new

    my $json = JSON::Tiny->new;

Instantiate a JSON::Tiny object.

=head2 decode

    my $array = $json->decode($bytes);
    my $hash  = $json->decode($bytes);

Decode JSON to Perl data structure and return C<undef> if decoding fails.

=head2 encode

    my $bytes = $json->encode([1, 2, 3]);
    my $bytes = $json->encode({foo => 'bar'});

Encode Perl data structure to JSON.

=head2 false

    my $false = JSON::Tiny->false;
    my $false = $json->false;

False value, used because Perl has no native equivalent.

=head2 true

    my $true = JSON::Tiny->true;
    my $true = $json->true;

True value, used because Perl has no native equivalent.

=head3 More on Booleans

A reference to a scalar (even if blessed) will also be 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 stringification and numeric overloading. As an
B<advanced option>, if the user requires a plain old literal C<0> or C<1>,
setting C<$JSON::Tiny::FALSE = 0;> and C<$JSON::Tiny::TRUE = 1;>, or some
other value, including blessed references prior to calling the C<decode> method
will have the desired effect. Use C<local> to prevent the change from causing
errors at a distance.

=head1 Tiny

Comparing JSON::Tiny with JSON::PP from the L<JSON> distribution:

=over 4

=item * L<JSON::PP> is configurable, but more complex. L<JSON::Tiny> offers
sane defaults, and zero configuration.

=item * Installation with C<cpanm>: L<JSON::PP>, 5.2 seconds. L<JSON::Tiny>,
1.9 seconds (including download).

=item * Minimal Dependencies: Both L<JSON::PP> and L<JSON::Tiny> only use core
dependencies. JSON::Tiny requires Perl 5.8.4, while L<JSON::PP> requires 5.6.

=item * Simple Design: L<JSON> has 2254 lines of code in six modules and five
files, and a dist tarball of 84KB. L<JSON::Tiny> has under 350 lines of code; a
single module in a single file -- great for embedding. The tarball is 19KB.

=item * L<JSON::PP> has 42 functions and methods. L<JSON::Tiny> has seven.

=item * Performance (Benchmarks):

               Rate   JSON_PP JSON_Tiny
    JSON_PP   288/s        --      -62%
    JSON_Tiny 767/s      166%        --

The benchmark code is included in this distribution's C<examples/> folder.
L<JSON> will automatically use L<JSON::XS> if it's available, in which case
L<JSON::XS> wins.

JSON::Tiny's lightweight design also reduces its startup time as compared to
the L<JSON> module. This may be beneficial in applications such as CGI, where
processes are started frequently.

=item * Light Memory Needs: Memory usage was tested with
L<http://valgrind.org/valgrind> and L<Devel::MemoryTrace::Lite> by running
C<examples/json_pp_alone.pl> and C<examples/json_tiny_alone.pl>.

=over 4

=item * L<JSON> (JSON::PP) -- Devel::MemoryTrace::Lite: 1.7MB. valgrind: 6.1MB.

=item * L<JSON::Tiny> -- Devel::MemoryTrace::Lite: 1.1MB. valgrind: 5.4MB.

=back

These utilities show that JSON::Tiny is 600-700KB smaller than JSON::PP.

=back

=head1 CONFIGURATION AND ENVIRONMENT

No configuration.

=head1 DEPENDENCIES

Perl 5.8.4 or newer.

=head1 INCOMPATIBILITIES

Incompatible with L<Exporter> versions that predate Perl 5.8.4.  Perl 5.8.4
shipped with Exporter v5.58.  Exporter became dual-life as of v5.59. Upgrade
Exporter to v5.59 or newer for Perl versions older than 5.8.4.

=head1 AUTHOR

David Oswald, C<< <davido at cpan.org> >>

Code and tests were adapted from L<Mojo::JSON>.

=head1 SUPPORT

Directed 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:

=over 4

=item * Github: Development is hosted on Github at:

L<http://www.github.com/daoswald/JSON-Tiny>

=item * RT: CPAN's request tracker (report bugs here)

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=JSON-Tiny>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/JSON-Tiny>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/JSON-Tiny>

=item * Search CPAN

L<http://search.cpan.org/dist/JSON-Tiny/>

=back

=head1 ACKNOWLEDGEMENTS

The L<Mojolicious> team for producing an excellent product that offers
light-weight implementations of many useful tools. This module was adapted
directly from L<Mojo::JSON>, which was chosen as a model because it is robust,
minimal, and well tested. Mojo::JSON's tests were also adapted to a
dependency-free design.

Christian Hansen, whos L<GitHub Gist|https://gist.github.com/chansen/810296>
provided the basis for L<Mojo::JSON>, from which this module is derived.

Randal Schwartz for showing Los Angeles Perl Mongers (Sept 2012) his
pure-regexp JSON parser (L<PerlMonks|http://perlmonks.org/?node_id=995856>).
He wasn't involved in JSON::Tiny, but it was exploring alternatives to his
solution that led to this fork of Mojo::JSON.

=head1 LICENSE AND COPYRIGHT

Copyright 2012-2013 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 L<http://www.perlfoundation.org/artistic_license_2_0> for more information.

=head1 SEE ALSO

L<JSON>, L<JSON::PP>, L<JSON::XS>, L<Mojo::JSON>.

=cut
