Hi Monks,

I am trying to serialise and reload a Moose object structure. I would like to build my object structure, dump this out to a JSON, then load a JSON that may have some of the values modified and have these changes propagated through my object structure.

I tried do this with MooseX::Storage, however when I load the modified JSON the changed value is not reflected in the Moose object

In the example below it looks like the incoming json is being ignored. Have I missed something or misunderstood how load works?
#!/usr/bin/perl -w #------------------------------------------------ package myDevice; use strict; use Moose; use MooseX::Storage; with Storage( 'format' => 'JSON', 'io' => 'File' ); #with Storage( 'format' => [ JSONpm => { json_opts => { pretty => 1 } +} ], 'io' => 'File' ); has 'instruments' => ( isa => 'HashRef', is => 'rw', default => sub { +{} } ); sub BUILD { my $self = shift; my $noOfInstruments = keys(%{$self->instruments}) . "\n"; if ( $noOfInstruments > 0 ) { print "The object seems to exist already\n"; } else { $self->instruments->{batt1} = readBatteryVoltage->new(); $self->instruments->{batt2} = readBatteryVoltage->new(); $self->instruments->{batt3} = readBatteryVoltage->new(); } return $self; } 1; #------------------------------------------------ package readBatteryVoltage; use strict; use Moose; use MooseX::Storage; with Storage( 'format' => 'JSON', 'io' => 'File' ); #with Storage( 'format' => [ JSONpm => { json_opts => { pretty => 1 } +} ], 'io' => 'File' ); has 'randomNumber' => ( is => 'rw', default => 1234567, ); has 'var2' => ( is => 'rw', default => 1999, ); has 'str1' => ( is => 'rw', default => "I am a string", ); 1; #------------------------------------------------ my $aNewDevice = myDevice->new(); # create our device print "+++++++++++++++Initial object we create\n"; print $aNewDevice->freeze() . "\n"; # look at the output # Its should look as below #{"__CLASS__":"myDevice","instruments":{"batt":{"__CLASS__":"readBatte +ryVoltage","randomNumber":1234567}}} $aNewDevice->store('file1.json'); # save our object # Lets quickly modify the value system 'perl -p -i -e \'s/1234567/987654321/\' file1.json'; system 'perl -p -i -e \'s/1234567/10293847/\' file1.json'; # remove a value to have a partial JSON system 'perl -p -i -e \'s/,\"var2\":1999//g\' file1.json'; # Change this also system 'perl -p -i -e \'s/am a string/am a number/\' file1.json'; system 'perl -p -i -e \'s/am a string/am a modified variable/\' file1. +json'; print "+++++++++++++++++File after modification\n"; system 'more file1.json'; # Its should look as below #{"__CLASS__":"myDevice","instruments":{"batt":{"__CLASS__":"readBatte +ryVoltage","randomNumber":987654321}}} my $anotherNewDevice = myDevice->load('file1.json'); use Data::Dumper; print "+++++++++++++++++Using Dumper\n"; print Dumper($anotherNewDevice); print "+++++++++++++++++Our new device\n"; print $anotherNewDevice->freeze() . "\n"; # Why is randomNumber not taking the vaue 987654321? #{"__CLASS__":"myDevice","instruments":{"batt":{"__CLASS__":"readBatte +ryVoltage","randomNumber":1234567}}}

In reply to MooseX::Storage JSON load not functioning as expected by irishBatman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.