Hi,
good suggestion, but unfortunately this gives me the same error as with fields.
Here is a example of the problem:
use Test::More;
use_ok('Data::Dumper');
use_ok('JSON');
{
package Wrapper;
use fields qw (
_value
);
sub new{
my $s = $_[0];
unless (ref $s) {
$s = fields::new($s);
}
$s->{_value} = 42;
return $s;
}
sub TO_JSON{
return $_[0]->{_value};
}
}
my $t = Wrapper->new();
my $jsonString = undef;
eval{
my $json = JSON->new;
$json->allow_nonref(1);
$json->utf8(1);
$json->convert_blessed(1);
$json->allow_blessed(1);
$jsonString = $json->encode($t);
};
if($@){
print(Dumper($@));
fail("We got an error");
}
print $jsonString;
done_testing();
And the output:
ok 1 - use Data::Dumper;
# Failed test 'We got an error'
ok 2 - use JSON;
$VAR1 = 'Modification of a read-only value attempted at /pathtofile/JS
+ON::XS-Fields.t line 34.
';
not ok 3 - We got an error
1..3
|