Subject: | cannot handle URI class (blessed scalar ref) |
This script fails:
#!/usr/bin/perl
use JSON;
use URI;
print objToJson({uri => URI->new('http://www.slashdot.org/')},
{convblessed => 1}), "\n";
The attached diff fixes the problem by converting the URI::href object
into a simple scalar. The conversion is admittedly very naive but I
think most blessed scalars will be sensible self-representations of
their object data.
Subject: | json.diff |
--- /usr/lib/perl5/site_perl/5.8.8/JSON/Converter.pm 2006-06-04 15:00:44.000000000 +0100
+++ /tmp/Converter.pm 2007-03-05 19:58:28.000000000 +0000
@@ -169,7 +169,6 @@
return $obj->toJson($self);
}
-
sub _valueToJson {
my ($self, $value) = @_;
@@ -317,8 +316,10 @@
sub _blessedToNormal {
my $type = _getObjType($_[0]);
- return $type eq 'HASH' ? _blessedToNormalHash($_[0]) :
- $type eq 'ARRAY' ? _blessedToNormalArray($_[0]) : $_[0];
+ return $type eq 'HASH' ? _blessedToNormalHash($_[0]) :
+ $type eq 'ARRAY' ? _blessedToNormalArray($_[0]) :
+ $type eq 'SCALAR' ? _blessedToNormalScalar($_[0]) :
+ $_[0];
}
@@ -359,6 +360,22 @@
return \@res;
}
+sub _blessedToNormalScalar {
+ my ($obj) = @_;
+ my $res;
+
+ die "circle ref!" if(grep { overload::AddrRef($_) eq overload::AddrRef($obj) }
+ @JSON::Converter::_blessedToNormal::obj_addr);
+
+ push @JSON::Converter::_blessedToNormal::obj_addr, $obj;
+
+ $res = _blessedToNormal($$obj);
+
+ pop @JSON::Converter::_blessedToNormal::obj_addr;
+
+ return $res; # JSON can't really do scalar refs so it can't be \$res
+}
+
##############################################################################
1;
__END__