diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index 03426414e..de15930f6 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -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!"); diff --git a/t/Asset/JSONCollateral.t b/t/Asset/JSONCollateral.t index e3f235be7..597f4b018 100644 --- a/t/Asset/JSONCollateral.t +++ b/t/Asset/JSONCollateral.t @@ -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 => [ ], });