JSON::XS::VersionOneAndTwo - Support versions 1 and 2 of JSON::XS


JSON-XS-VersionOneAndTwo documentation Contained in the JSON-XS-VersionOneAndTwo distribution.

Index


Code Index:

NAME

Top

JSON::XS::VersionOneAndTwo - Support versions 1 and 2 of JSON::XS

SYNOPSIS

Top

  use JSON::XS::VersionOneAndTwo;
  my $data = { 'three' => [ 1, 2, 3 ] };

  # use JSON::XS version 1.X style
  my $json1 = to_json($data);
  my $data1 = from_json($json1);

  # or use JSON::XS version 2.X style
  my $json2 = encode_json($data);
  my $data2 = decode_json($json2);

DESCRIPTION

Top

JSON::XS is by far the best JSON module on the CPAN. However, it changed its API at version 2.01. If you have to maintain code which may be run on systems with either version one or two then this is a bit of a pain. This module takes the pain away without sacrificing performance.

AUTHOR

Top

Leon Brocard <acme@astray.com>.

COPYRIGHT

Top


JSON-XS-VersionOneAndTwo documentation Contained in the JSON-XS-VersionOneAndTwo distribution.

package JSON::XS::VersionOneAndTwo;
use strict;
no strict 'refs';
use warnings;
use JSON::XS;
our $VERSION = '0.31';

sub import {
    my ( $exporter, @imports ) = @_;
    my ( $caller, $file, $line ) = caller;
    my $json_xs_version = $JSON::XS::VERSION;
    if ( $json_xs_version < 2.01 ) {
        *{ $caller . '::encode_json' } = \&JSON::XS::to_json;
        *{ $caller . '::to_json' }     = \&JSON::XS::to_json;
        *{ $caller . '::decode_json' } = \&JSON::XS::from_json;
        *{ $caller . '::from_json' }   = \&JSON::XS::from_json;
    } else {
        *{ $caller . '::encode_json' } = \&JSON::XS::encode_json;
        *{ $caller . '::to_json' }     = \&JSON::XS::encode_json;
        *{ $caller . '::decode_json' } = \&JSON::XS::decode_json;
        *{ $caller . '::from_json' }   = \&JSON::XS::decode_json;
    }
}

1;

__END__