Skip Menu |

This queue is for tickets about the PHP-Serialization CPAN distribution.

Report information
The Basics
Id: 58047
Status: open
Worked: 30 min
Priority: 0/
Queue: PHP-Serialization

People
Owner: Nobody in particular
Requestors: perl [...] toby.ink
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 0.34
Fixed in: (no value)



Subject: does not export blessed arrayrefs
Currently PHP::Serializer::_encode->type('obj', $val) assumes that $val is a blessed hashref. In fact, it may be a blessed arrayref, blessed scalar ref, or something else. The JSON module uses this trick: require B; *UNIVERSAL::TO_JSON = sub { my $b_obj = B::svref_2object( $_[0] ); return $b_obj->isa('B::HV') ? { %{ $_[0] } } : $b_obj->isa('B::AV') ? [ @{ $_[0] } ] : undef ; } Then, when asked to serialize a blessed object, it instead serializes the result of $object->TO_JSON.
I'd welcome a patch to do this, but I'm unlikely to get time to do this myself.
On 2010-06-15T23:19:01+01:00, BOBTFISH wrote: Show quoted text
> I'd welcome a patch to do this, but I'm unlikely to get time to do this > myself.
This should do the trick: http://goddamn.co.uk/viewvc/perlmods/PHP-Serialization/non-hash- objects.patch
Subject: non-hash-objects.patch
--- /usr/lib/perl5/site_perl/5.10.1/PHP/Serialization.pm 2010-03-18 22:23:41.000000000 +0000 +++ Serialization.pm 2010-06-16 16:46:35.058090594 +0100 @@ -1,6 +1,7 @@ package PHP::Serialization; use strict; use warnings; +use B (); use Exporter (); use Scalar::Util qw/blessed/; use Carp qw(croak confess carp); @@ -448,14 +449,37 @@ } } elsif ( $type eq 'obj' ) { + # hijack the JSON module's method for converting objects to non-objects. + if ($val->can('TO_JSON')) { + return $self->encode($val->TO_JSON); + } my $class = ref($val); $class =~ /(\w+)$/; my $subclass = $1; - $buffer .= sprintf('O:%d:"%s":%d:', length($subclass), $subclass, scalar(keys %{$val})) . '{'; - foreach ( %{$val} ) { - $buffer .= $self->encode($_); + my $b_obj = B::svref_2object( $val ); + if ( $b_obj->isa('B::HV') ) { + $buffer .= sprintf('O:%d:"%s":%d:', length($subclass), $subclass, scalar(keys %{$val})) . '{'; + foreach ( %{$val} ) { + $buffer .= $self->encode($_); + } + $buffer .= '}'; + } + elsif ($b_obj->isa('B::AV')) { + # PHP has no equivalent for blessed arrayrefs, so just encode as an array. + $buffer .= sprintf('a:%d:',($#{$val}+1)) . '{'; + map { # Ewww + $buffer .= $self->encode($_); + $buffer .= $self->encode($$val[$_]); + } 0..$#{$val}; + $buffer .= '}'; + } + elsif ($b_obj->isa('B::PVMG')) { + # PHP has no equivalent for blessed scalars, so just encode as a string. + $buffer .= sprintf('s:%d:"%s";', length($$val), $$val); + } + else { + confess "Unknown object type!"; } - $buffer .= '}'; } else { confess "Unknown encode type!";
I don't see any tests that this actually works or does anything, and certainly none that it can round-trip?
On 2010-06-16T16:54:33+01:00, BOBTFISH wrote: Show quoted text
> I don't see any tests that this actually works or does anything, and > certainly none that it can round-trip?
Here are my tests: http://goddamn.co.uk/viewvc/perlmods/PHP-Serialization/test.pl Not in a suitable format for "t/" I'm afraid. Serializing non-hashref objects (e.g. blessed arrayrefs, blessed scalarrefs) are not round-trippable - PHP doesn't contain such structures, so when they're unserialised, they come back as unblessed arrayrefs and as strings respectively.
On Wed Jun 16 12:28:44 2010, TOBYINK wrote: Show quoted text
> On 2010-06-16T16:54:33+01:00, BOBTFISH wrote:
> > I don't see any tests that this actually works or does anything, and > > certainly none that it can round-trip?
> > Here are my tests: > > http://goddamn.co.uk/viewvc/perlmods/PHP-Serialization/test.pl > > Not in a suitable format for "t/" I'm afraid. > > Serializing non-hashref objects (e.g. blessed arrayrefs, blessed > scalarrefs) are not round-trippable - PHP doesn't contain such > structures, so when they're unserialised, they come back as unblessed > arrayrefs and as strings respectively.
Shouldn't these be made into php-objects when you serialize them?