Check JSON deserialization, and make Assets do it.

This commit is contained in:
Colin Kuskie 2009-08-26 17:13:00 +00:00
parent dc99ee12aa
commit 13cceb9c1a
2 changed files with 30 additions and 1 deletions

View file

@ -1770,6 +1770,13 @@ sub new {
if (defined $properties) {
my $object = { _session=>$session, _properties => $properties };
bless $object, $class;
foreach my $definition (@{ $object->definition($session) }) {
foreach my $property (keys %{ $definition->{properties} }) {
if ($definition->{properties}->{$property}->{serialize} && $object->{_properties}->{$property} ne '') {
$object->{_properties}->{$property} = JSON->new->canonical->decode($object->{_properties}->{$property});
}
}
}
return $object;
}
$session->errorHandler->error("Something went wrong trying to instanciate a '$className' with assetId '$assetId', but I don't know what!");

View file

@ -12,6 +12,14 @@ use FindBin;
use strict;
use lib "$FindBin::Bin/../lib";
################################################################
#
# Checks that Assets can use WebGUI::JSONCollateral, that assets
# can automatically serialize and deserialize data structures
# and other asset functions work correctly.
#
################################################################
use WebGUI::Test;
use WebGUI::Session;
use WebGUI::Asset;
@ -38,7 +46,7 @@ create table jsonCollateralDummy (
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
EOSQL
plan tests => 39;
plan tests => 40;
my $asset = WebGUI::Asset->getDefault($session)->addChild({
className => 'WebGUI::Asset::JSONCollateralDummy',
@ -83,6 +91,20 @@ cmp_deeply(
'get returns a hash ref with data in it'
);
################################################################
#
# Checking Asset deserialization
#
################################################################
my $assetClone = WebGUI::Asset->new($session, $asset->getId, 'WebGUI::Asset::JSONCollateralDummy');
cmp_deeply(
$assetClone->get('jsonField'),
[ { alpha => "aye", beta => "bee"} ],
'new deserializes data from the db'
);
$asset->update({
jsonField => [ ],
});