Hello,
I don't know if this exists already, but I needed a way to serialize an
entire rose object tree into json. To do this, I created the below
methods in my base Rose::DB::Object class.
I can then use this to serialize the entire object tree by doing:
my $json = JSON::Syck::Dump($rose_object->object_as_data_structure);
Hopefully this will be useful to some. This isn't limited to json of
course, the returned value from object_as_data_structure is just a
hashref of the rose object (including any loaded relationships) with all
references removed.
It is a work in progress. It may not handle all column types.
Here's the code:
=head2 object_as_data_structure
Returns a hashref of the rose object in a serializable form.
All references are removed.
The returned data structure is a copy of the object's values
and so will not be updated when the rose object itself is updated.
=cut
sub object_as_data_structure
{
my ($self) = @_;
my $data = {};
$self->_process_object_as_data_structure($self, $data);
return $data;
}
sub _process_object_as_data_structure
{
my ($self, $object, $data) = @_;
my $meta = $object->meta;
my $object_type = ref($object);
#warn "object_as_data_structure: processing $object_type";
# add the column values
foreach my $col ( $meta->column_names )
{
my $value = $object->$col();
my $type = ref($value);
# got back a string/number, just add it to the $data
if ( ! $type )
{
#warn "object_as_data_structure -- col: $col, value: $value";
$data->{$col} = $value;
}
# this is an object
elsif ( $type !~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE)$/ )
{
#warn "object_as_data_structure -- col: $col, type: $type";
# this is a date field
if ( $type eq 'DateTime' )
{
my $real_value = $value->ymd . ' ' . $value->hms;
#warn "object_as_data_structure -- col: $col, value(from
DateTime): $real_value";
$data->{$col} = $real_value;
}
# try to get the text value from the ->name method
elsif ( $value->can('name') )
{
my $name = $value->name;
my $real_value = $value->$name();
my $real_type = ref($real_value);
if ( !$real_type )
{
#warn "object_as_data_structure -- col: $col,
value(from ->$name): $real_value";
$data->{$col} = $real_value;
}
else
{
#warn "object_as_data_structure -- col: $col, cannot
determine the value of $real_type objects";
}
}
else
{
#warn "object_as_data_structure -- col: $col, unknown
data type: $type";
}
}
else
{
#warn "object_as_data_structure -- col: $col, cannot
determine the value of $type objects";
}
}
# add any loaded relationships
foreach my $rel ( $meta->relationships )
{
my $rel_name = $rel->name;
if ( $object->has_loaded_related($rel_name) )
{
$data->{$rel_name} = {};
$self->_process_object_as_data_structure($object->$rel_name,
$data->{$rel_name});
}
}
}
- Ken (perl--AT--xev--DOT--net)
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Rose-db-object mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rose-db-object