package JSON::Meth;

use strict;
use warnings;

our $VERSION = '1.001002'; # VERSION

use JSON::MaybeXS;
use Carp;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($j);

my $data;

use overload q{""}  => sub { $data },
             q{@{}} => sub { $data },
             q{%{}} => sub { $data };

our $j = bless sub {
    my $in = shift;
    return $data = decode_json $in
        unless ref $in;

    return $data = encode_json $in
        unless ref $in eq 'JSON::Meth';

    defined $data
    or croak 'You tried to call $j->$j, but $j does not have any data '
        . ' stored. You need at least one encode/decode call prior to '
        . ' this, for $j->$j to work.';

    return $data = decode_json $data
        unless ref $data;

    return $data = encode_json $data;
}, 'JSON::Meth';

q{
    Q: How many programmers does it take to change a light bulb?
    A: None. It's a hardware problem.
};

__END__

=encoding utf8

=for stopwords Znet Zoffix JSON  postfix

=head1 NAME

JSON::Meth - no nonsense JSON encoding/decoding as method calls on data

=head1 SYNOPSIS

=for html  <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-code.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">

    use JSON::Meth;

    # encode JSON:
    my $json_string = { my => 'data', foo => [ 'bar' ] }->$j;

    # decode JSON
    my $perl_structure = '["look","ma!","no","vars"]'->$j;

    # encode and interpolate $j in a string to get the result
    { my => 'data' }->$j;
    say "Look ma, JSON: $j";

    # decode and grab a piece of data, as if $j were a hashref:
    my $data = '{"my":"data"}'->$j->{my}; # $data contains string "data" now

    # just pretend $j is an arrayref:
    '["woo","hoo!"]'->$j;
    say for @$j;

    # go nuts! (outputs JSON string '["bar",{"ber":"beer"}]')
    say '{"foo":["bar",{"ber":"beer"}]}'->$j->{foo}->$j

    # event this works!! Meth? Not even once!
    say '["woo","hoo!"]'->$j->$j->$j->$j

=for html  </div></div>

=head1 DESCRIPTION

Don't make me think and give me what I want! This module automatically
figures out whether you want to encode a Perl data structure to JSON
or decode a JSON string to a Perl data structure.

The name C<JSON::Meth> is formed from
C<B<Meth>od>, which is the distinctive feature of this module.

=head1 EXPORTS

=head2 C<$j> variable

The module exports a single variable C<$j>. To encode/decode JSON,
simply make a method call on your data, with C<$j> as
the name of the method (see SYNOPSIS).

=head2 THE MAGIC

The result of the last decode/encode operation is stored internally
by the module and you can access that data by using C<$j> variable
as if it contained that result. To get the results of B<encode> operation,
simply stringify C<$j> (e.g. by interpolating it: C<"$j">).

=head1 PREFIX/POSTFIX

If you're not a fan of postfix decoding, just use C<$j> as a prefix call:

    # encode JSON:
    my $json_string = $j->( { my => 'data', foo => [ 'bar' ] } );

    # decode JSON
    my $perl_structure = $j->( '["look","ma!","no","vars"]' );

=for html <div style="background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/hr.png);height: 18px;"></div>

=head1 REPOSITORY

=for html  <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-github.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">

Fork this module on GitHub:
L<https://github.com/zoffixznet/JSON-Meth>

=for html  </div></div>

=head1 BUGS

=for html  <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-bugs.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">

To report bugs or request features, please use
L<https://github.com/zoffixznet/JSON-Meth/issues>

If you can't access GitHub, you can email your request
to C<bug-json-meth at rt.cpan.org>

=for html  </div></div>

=head1 AUTHOR

=for html  <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-author.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">

=for html   <span style="display: inline-block; text-align: center;"> <a href="http://metacpan.org/author/ZOFFIX"> <img src="http://www.gravatar.com/avatar/328e658ab6b08dfb5c106266a4a5d065?d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2F627d83ef9879f31bdabf448e666a32d5" alt="ZOFFIX" style="display: block; margin: 0 3px 5px 0!important; border: 1px solid #666; border-radius: 3px; "> <span style="color: #333; font-weight: bold;">ZOFFIX</span> </a> </span>

=for html  </div></div>

=head1 LICENSE

You can use and distribute this module under the same terms as Perl itself.
See the C<LICENSE> file included in this distribution for complete
details.

=cut