<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package JSON::MaybeXS;

use strict;
use warnings FATAL =&gt; 'all';
use base qw(Exporter);

our $VERSION = '1.004008';
$VERSION =~ tr/_//d;

sub _choose_json_module {
    return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'};
    return 'JSON::XS'         if $INC{'JSON/XS.pm'} &amp;&amp; eval { JSON::XS-&gt;VERSION(3.0); 1 };

    my @err;

    return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; };
    push @err, "Error loading Cpanel::JSON::XS: $@";

    return 'JSON::XS' if eval { require JSON::XS; JSON::XS-&gt;VERSION(3.0); 1; };
    push @err, "Error loading JSON::XS: $@";

    return 'JSON::PP' if eval { require JSON::PP; 1 };
    push @err, "Error loading JSON::PP: $@";

    die join( "\n", "Couldn't load a JSON module:", @err );

}

BEGIN {
    our $JSON_Class = _choose_json_module();
    $JSON_Class-&gt;import(qw(encode_json decode_json));
    no strict 'refs';
    *$_ = $JSON_Class-&gt;can($_)
      for qw(true false);
}

our @EXPORT = qw(encode_json decode_json JSON);
my @EXPORT_ALL = qw(is_bool);
our @EXPORT_OK = qw(is_bool to_json from_json);
our %EXPORT_TAGS = ( all =&gt; [ @EXPORT, @EXPORT_ALL ],
                     legacy =&gt; [ @EXPORT, @EXPORT_OK ],
                   );

sub JSON () { our $JSON_Class }

sub new {
  shift;
  my %args = @_ == 1 ? %{$_[0]} : @_;
  my $new = (our $JSON_Class)-&gt;new;
  $new-&gt;$_($args{$_}) for keys %args;
  return $new;
}

use Scalar::Util ();
use constant HAVE_BUILTIN =&gt; "$]" &gt;= 5.036;
use if HAVE_BUILTIN, experimental =&gt; 'builtin';

sub is_bool {
  die 'is_bool is not a method' if @_ &gt; 1;

  HAVE_BUILTIN and builtin::is_bool($_[0])
  or
  !!Scalar::Util::blessed($_[0])
    and ($_[0]-&gt;isa('JSON::PP::Boolean')
      or $_[0]-&gt;isa('Cpanel::JSON::XS::Boolean')
      or $_[0]-&gt;isa('JSON::XS::Boolean'));
}

# (mostly) CopyPasta from JSON.pm version 2.90
use Carp ();

sub from_json ($@) {
    if ( ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/ ) {
        Carp::croak "from_json should not be called as a method.";
    }
    my $json = JSON()-&gt;new;

    if (@_ == 2 and ref $_[1] eq 'HASH') {
        my $opt  = $_[1];
        for my $method (keys %$opt) {
            $json-&gt;$method( $opt-&gt;{$method} );
        }
    }

    return $json-&gt;decode( $_[0] );
}

sub to_json ($@) {
    if (
        ref($_[0]) =~ /^JSON/
        or (@_ &gt; 2 and $_[0] =~ /^JSON/)
          ) {
               Carp::croak "to_json should not be called as a method.";
    }
    my $json = JSON()-&gt;new;

    if (@_ == 2 and ref $_[1] eq 'HASH') {
        my $opt  = $_[1];
        for my $method (keys %$opt) {
            $json-&gt;$method( $opt-&gt;{$method} );
        }
    }

    $json-&gt;encode($_[0]);
}

1;

=head1 NAME

JSON::MaybeXS - Use L&lt;Cpanel::JSON::XS&gt; with a fallback to L&lt;JSON::XS&gt; and L&lt;JSON::PP&gt;

=head1 SYNOPSIS

  use JSON::MaybeXS;

  my $data_structure = decode_json($json_input);

  my $json_output = encode_json($data_structure);

  my $json = JSON()-&gt;new;

  my $json_with_args = JSON::MaybeXS-&gt;new(utf8 =&gt; 1); # or { utf8 =&gt; 1 }

=head1 DESCRIPTION

This module first checks to see if either L&lt;Cpanel::JSON::XS&gt; or
L&lt;JSON::XS&gt; (at at least version 3.0)
is already loaded, in which case it uses that module. Otherwise
it tries to load L&lt;Cpanel::JSON::XS&gt;, then L&lt;JSON::XS&gt;, then L&lt;JSON::PP&gt;
in order, and either uses the first module it finds or throws an error.

It then exports the C&lt;encode_json&gt; and C&lt;decode_json&gt; functions from the
loaded module, along with a C&lt;JSON&gt; constant that returns the class name
for calling C&lt;new&gt; on.

If you're writing fresh code rather than replacing L&lt;JSON.pm|JSON&gt; usage, you might
want to pass options as constructor args rather than calling mutators, so
we provide our own C&lt;new&gt; method that supports that.

=head1 EXPORTS

C&lt;encode_json&gt;, C&lt;decode_json&gt; and C&lt;JSON&gt; are exported by default; C&lt;is_bool&gt;
is exported on request.

To import only some symbols, specify them on the C&lt;use&gt; line:

  use JSON::MaybeXS qw(encode_json decode_json is_bool); # functions only

  use JSON::MaybeXS qw(JSON); # JSON constant only

To import all available sensible symbols (C&lt;encode_json&gt;, C&lt;decode_json&gt;, and
C&lt;is_bool&gt;), use C&lt;:all&gt;:

  use JSON::MaybeXS ':all';

To import all symbols including those needed by legacy apps that use L&lt;JSON::PP&gt;:

  use JSON::MaybeXS ':legacy';

This imports the C&lt;to_json&gt; and C&lt;from_json&gt; symbols as well as everything in
C&lt;:all&gt;.  NOTE: This is to support legacy code that makes extensive
use of C&lt;to_json&gt; and C&lt;from_json&gt; which you are not yet in a position to
refactor.  DO NOT use this import tag in new code, in order to avoid
the crawling horrors of getting UTF-8 support subtly wrong.  See the
documentation for L&lt;JSON&gt; for further details.

=head2 encode_json

This is the C&lt;encode_json&gt; function provided by the selected implementation
module, and takes a perl data structure which is serialised to JSON text.

  my $json_text = encode_json($data_structure);

=head2 decode_json

This is the C&lt;decode_json&gt; function provided by the selected implementation
module, and takes a string of JSON text to deserialise to a perl data structure.

  my $data_structure = decode_json($json_text);

=head2 to_json

This function is equivalent to calling C&lt;&lt; JSON()-&gt;new-&gt;encode($data_structure) &gt;&gt;.
It takes a perl data structure which is serialised to JSON text without encoding
it to UTF-8. You should only use this function if you expect another layer to
handle the UTF-8 encoding of the resulting JSON text.

  my $json_text = to_json($data_structure);

Additional arguments can be passed and will be handled as in L&lt;JSON/to_json&gt;,
this is included to support legacy code B&lt;only&gt;.

=head2 from_json

This function is equivalent to calling C&lt;&lt; JSON()-&gt;new-&gt;decode($json_text) &gt;&gt;. It
takes a string of unencoded JSON text to deserialise to a perl data structure. You
should only use this function if another layer is already handling the UTF-8
decoding of the input JSON text.

  my $data_structure = from_json($json_text);

Additional arguments can be passed and will be handled as in L&lt;JSON/from_json&gt;,
this is included to support legacy code B&lt;only&gt;.

=head2 JSON

The C&lt;JSON&gt; constant returns the selected implementation module's name for
use as a class name - so:

  my $json_obj = JSON()-&gt;new; # returns a Cpanel::JSON::XS or JSON::PP object

and that object can then be used normally:

  my $data_structure = $json_obj-&gt;decode($json_text); # etc.

The use of parentheses here is optional, and only used as a hint to the reader
that this use of C&lt;JSON&gt; is a I&lt;subroutine&gt; call, I&lt;not&gt; a class name.

=head2 is_bool

  $is_boolean = is_bool($scalar)

Returns true if the passed scalar represents either C&lt;true&gt; or
C&lt;false&gt;, two constants that act like C&lt;1&gt; and C&lt;0&gt;, respectively
and are used to represent JSON C&lt;true&gt; and C&lt;false&gt; values in Perl.

Since this is a bare sub in the various backend classes, it cannot be called as
a class method like the other interfaces; it must be called as a function, with
no invocant.  It supports the representation used in all JSON backends.

Available since version 1.002004.

=head1 CONSTRUCTOR

=head2 new

With L&lt;JSON::PP&gt;, L&lt;JSON::XS&gt; and L&lt;Cpanel::JSON::XS&gt; you are required to call
mutators to set options, such as:

  my $json = $class-&gt;new-&gt;utf8(1)-&gt;pretty(1);

Since this is a trifle irritating and noticeably un-perlish, we also offer:

  my $json = JSON::MaybeXS-&gt;new(utf8 =&gt; 1, pretty =&gt; 1);

which works equivalently to the above (and in the usual tradition will accept
a hashref instead of a hash, should you so desire).

The resulting object is blessed into the underlying backend, which offers (at
least) the methods C&lt;encode&gt; and C&lt;decode&gt;.

=head1 BOOLEANS

To include JSON-aware booleans (C&lt;true&gt;, C&lt;false&gt;) in your data, just do:

    use JSON::MaybeXS;
    my $true = JSON()-&gt;true;
    my $false = JSON()-&gt;false;

The booleans are also available as subs or methods on JSON::MaybeXS.

    use JSON::MaybeXS ();
    my $true = JSON::MaybeXS::true;
    my $true = JSON::MaybeXS-&gt;true;
    my $false = JSON::MaybeXS::false;
    my $false = JSON::MaybeXS-&gt;false;

=head1 CONVERTING FROM JSON::Any

L&lt;JSON::Any&gt; used to be the favoured compatibility layer above the various
JSON backends, but over time has grown a lot of extra code to deal with legacy
backends (e.g. L&lt;JSON::Syck&gt;) that are no longer needed.  This is a rough guide of translating such code:

Change code from:

    use JSON::Any;
    my $json = JSON::Any-&gt;new-&gt;objToJson($data);    # or to_json($data), or Dump($data)

to:

    use JSON::MaybeXS;
    my $json = encode_json($data);


Change code from:

    use JSON::Any;
    my $data = JSON::Any-&gt;new-&gt;jsonToObj($json);    # or from_json($json), or Load($json)

to:

    use JSON::MaybeXS;
    my $json = decode_json($data);

=head1 CAVEATS

The C&lt;new()&gt; method in this module is technically a factory, not a
constructor, because the objects it returns will I&lt;NOT&gt; be blessed into the
C&lt;JSON::MaybeXS&gt; class.

If you are using an object returned by this module as a Moo(se) attribute,
this type constraint code:

    is 'json' =&gt; ( isa =&gt; 'JSON::MaybeXS' );

will I&lt;NOT&gt; do what you expect. Instead, either rely on the C&lt;JSON&gt; class
constant described above, as so:

    is 'json' =&gt; ( isa =&gt; JSON::MaybeXS::JSON() );

Alternatively, you can use duck typing:

    use Moose::Util::TypeConstraints 'duck_type';
    is 'json' =&gt; ( isa =&gt; Object , duck_type([qw/ encode decode /]));

=head1 INSTALLATION

At installation time, F&lt;Makefile.PL&gt; will attempt to determine if you have a
working compiler available, and therefore whether you are able to run XS code.
If so, L&lt;Cpanel::JSON::XS&gt; will be added to the prerequisite list, unless
L&lt;JSON::XS&gt; is already installed at a high enough version. L&lt;JSON::XS&gt; may
also be upgraded to fix any incompatibility issues.

Because running XS code is not mandatory and L&lt;JSON::PP&gt; (which is in perl
core) is used as a fallback backend, this module is safe to be used in a suite
of code that is fatpacked or installed into a restricted-resource environment.

You can also prevent any XS dependencies from being installed by setting
C&lt;PUREPERL_ONLY=1&gt; in F&lt;Makefile.PL&gt; options (or in the C&lt;PERL_MM_OPT&gt;
environment variable), or using the C&lt;--pp&gt; or C&lt;--pureperl&gt; flags with the
L&lt;cpanminus client|cpanm&gt;.

=head1 AUTHOR

mst - Matt S. Trout (cpan:MSTROUT) &lt;mst@shadowcat.co.uk&gt;

=head1 CONTRIBUTORS

=over 4

=item * Clinton Gormley &lt;drtech@cpan.org&gt;

=item * Karen Etheridge &lt;ether@cpan.org&gt;

=item * Kieren Diment &lt;diment@gmail.com&gt;

=back

=head1 COPYRIGHT

Copyright (c) 2013 the C&lt;JSON::MaybeXS&gt; L&lt;/AUTHOR&gt; and L&lt;/CONTRIBUTORS&gt;
as listed above.

=head1 LICENSE

This library is free software and may be distributed under the same terms
as perl itself.

=cut
</pre></body></html>