AMF-Perl
view release on metacpan or search on metacpan
lib/AMF/Perl/IO/Serializer.pm view on Meta::CPAN
# write the object name/value pairs
$self->writeObject($d);
}
# writes an object to the stream
sub writeObject
{
my ($self, $d)=@_;
# loop over each element
while ( my ($key, $data) = each %$d)
{
# write the name of the object
$self->{out}->writeUTF($key);
if ($self->{__columnTypes__} && $key eq "initialData")
{
$self->{__writingRecordset__} = 1;
}
# write the value of the object
$self->writeData($data);
$self->{__writingRecordset__} = 0;
}
# write the end object flag 0x00, 0x00, 0x09
$self->{out}->writeInt(0);
$self->{out}->writeByte(9);
}
# write an AMF object
# The difference with regular object is that the code is different
# and the class name is explicitly sent. Good for RecordSets.
sub writeAMFObject
{
my ($self, $object)=@_;
# write the custom package code
$self->{out}->writeByte(16);
# write the package name
$self->{out}->writeUTF($object->{_explicitType});
$self->{__columnTypes__} = $object->{__columnTypes__} if $object->{__columnTypes__};
# write the package's data
$self->writeObject($object);
delete $self->{__columnTypes__};
}
# main switch for dynamically determining the data type
# this may prove to be inadequate because perl isn't a typed
# language and some confusion may be encountered as we discover more data types
# to be passed back to flash
#All scalars are assumed to be strings, not numbers.
#Regular arrays and hashes are prohibited, as they are indistinguishable outside of perl context
#Only arrayrefs and hashrefs will work
# were still lacking dates, xml, and strings longer than 65536 chars
sub writeData
{
my ($self, $d, $type)=@_;
$type = "unknown" unless $type;
# **************** TO DO **********************
# Since we are now allowing the user to determine
# the datatype we have to validate the user's suggestion
# vs. the actual data being passed and throw an error
# if things don't check out.!!!!
# **********************************************
# get the type of the data by checking its reference name
#if it was not explicitly passed
if ($type eq "unknown")
{
if (!defined $d) # convert undef to null, but not "" or 0
{
$type = "NULL";
}
else
{
my $myRef = ref $d;
if (!$myRef || $myRef =~ "SCALAR")
{
if ($myRef) {
study $$myRef;
($type, $d) = $self->deduceType($$myRef);
} else {
($type, $d) = $self->deduceType($d);
}
}
elsif ($myRef =~ "ARRAY")
{
$type = "array";
}
elsif ($myRef =~ "HASH")
{
$type = "hash";
}
else
{
$type = "object";
}
}
}
#BOOLEANS
if ($type eq "boolean")
{
$self->writeBoolean($d);
}
#STRINGS
elsif ($type eq "string")
{
$self->writeString($d);
}
# DOUBLES
elsif ($type eq "double")
{
$self->writeNumber($d);
}
# INTEGERS
elsif ($type eq "integer")
{
$self->writeNumber($d);
}
# OBJECTS
( run in 0.702 second using v1.01-cache-2.11-cpan-39bf76dae61 )