Subject: | Allow JSON::Any to work with all versions of JSON::XS |
This fairly trivial patch allows JSON::Any to work round the API changes
in JSON::XS from v1 to v2.
It's fairly icky but it might be useful.
Thanks,
Simon
Subject: | json-any-xs.patch |
diff -urbP JSON-Any-1.15/Makefile.PL JSON-Any-1.15-all_xs/Makefile.PL
--- JSON-Any-1.15/Makefile.PL 2008-01-02 20:08:28.000000000 +0000
+++ JSON-Any-1.15-all_xs/Makefile.PL 2008-02-13 09:10:55.000000000 +0000
@@ -14,7 +14,7 @@
feature 'JSON::XS',
-default => 0,
- 'JSON::XS' => '2.01';
+ 'JSON::XS' => '0';
feature 'JSON::PC',
-default => 0,
diff -urbP JSON-Any-1.15/lib/JSON/Any.pm JSON-Any-1.15-all_xs/lib/JSON/Any.pm
--- JSON-Any-1.15/lib/JSON/Any.pm 2008-01-02 20:05:38.000000000 +0000
+++ JSON-Any-1.15-all_xs/lib/JSON/Any.pm 2008-02-13 09:20:21.000000000 +0000
@@ -93,7 +93,39 @@
},
},
- json_xs => {
+ json_xs_1 => {
+ encoder => 'to_json',
+ decoder => 'from_json',
+ create_object => sub {
+ my ($self, $conf) = @_;
+
+ my @params = qw(
+ ascii
+ utf8
+ pretty
+ indent
+ space_before
+ space_after
+ canonical
+ allow_nonref
+ shrink
+ max_depth
+ );
+
+ my $obj = $handler->new;
+ for my $mutator (@params) {
+ next unless exists $conf->{$mutator};
+ $obj = $obj->$mutator( $conf->{$mutator} );
+ }
+ $self->[ENCODER] = 'encode';
+ $self->[DECODER] = 'decode',
+ $self->[HANDLER] = $obj;
+ },
+ },
+
+
+
+ json_xs_2 => {
encoder => 'encode_json',
decoder => 'decode_json',
create_object => sub {
@@ -149,6 +181,16 @@
$conf{json_pc} = $conf{json};
}
+sub _make_key {
+ my $handler = shift;
+ ( my $key = lc($handler) ) =~ s/::/_/g;
+ if ('json_xs' eq $key) {
+ no strict 'refs';
+ $key.= "_".(split /\./, ${"$handler\::VERSION"})[0];
+ }
+ return $key;
+}
+
sub import {
my $class = shift;
my @order = @_;
@@ -164,7 +206,7 @@
eval "require $testmod";
unless ($@) {
$handler = $testmod;
- ( my $key = lc($handler) ) =~ s/::/_/g;
+ my $key = _make_key($handler);
$encoder = $conf{$key}->{encoder};
$decoder = $conf{$key}->{decoder};
last;
@@ -257,7 +299,7 @@
sub new {
my $class = shift;
my $self = bless [], $class;
- ( my $key = lc($handler) ) =~ s/::/_/g;
+ my $key = _make_key($handler);
if ( my $creator = $conf{$key}->{create_object} ) {
my @config = @_;
if ( $ENV{JSON_ANY_CONFIG} ) {