Wednesday, 28 August 2013

Remove __CLASS__ From JSON Output of Moose Object In Perl

Remove __CLASS__ From JSON Output of Moose Object In Perl

I'm working with moose objects in perl. I want to be able to covert the
moose objects I make directly to JSON.
However, when I use use MooseX::Storage to covert the objects, it includes
a hidden attribute that I don't know how to remove the "__ CLASS __ "
(please ignore the spaces).
Is there a way to remove this using MooseX::Storage ? (For now I am just
using MooseX::Storage to covert it and using JSON to remove the "__ CLASS
__ " attribute by going to a hash . )
package Example::Component;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON');
has 'description' => (is => 'rw', isa => 'Str');
1;
no Moose;
no MooseX::Storage;
use JSON;
my $componentObject = Example::Component->new;
$componentObject->description('Testing item with type');
my $jsonString = $componentObject->freeze();
my $json_obj = new JSON;
print $jsonString."\n\n";
my $perl_hash = $json_obj->decode ($jsonString);
delete ${$perl_hash}{'__CLASS__'};
$jsonString = $json_obj->encode($perl_hash);
print $jsonString."\n\n";

No comments:

Post a Comment