Here is a simple example of what I am attempting. I start with with MyClassA which will contain an instance of MyClassB:

package MyClassA; use strict; use warnings 'all'; sub new { my $classname = shift; my $self = {}; bless($self, $classname); my %extras = @_; @$self{keys %extras} = values %extras; return $self; } sub TO_JSON { my $self = shift; return { %{$self} }; } sub setClassB { my $self = shift; $self->{MY_CLASS_B} = shift; } sub getClassB { my $self = shift; return $self->{MY_CLASS_B}; } 1;

and ,

package MyClassB; use strict; use warnings 'all'; sub new { my $classname = shift; my $self = {}; bless($self, $classname); my %extras = @_; @$self{keys %extras} = values %extras; return $self; } sub TO_JSON { my $self = shift; return { %{$self} }; } sub getName { my $self = shift; return $self->{NAME}; } 1;

Next, we build and encode MyClassA:

use strict; use warnings 'all'; use JSON::MaybeXS ':all'; use MyClassA; use MyClassB; my $a = MyClassA->new(NAME => 'A instance'); my $b = MyClassB->new(NAME => 'B instance'); $a->setClassB($b); print JSON->new()->convert_blessed()->encode($a) . "\n";

And we get output that looks good:

{"NAME":"A instance","MY_CLASS_B":{"NAME":"B instance"}}

The trouble is seen when we try to read and use this JSON:

use strict; use warnings 'all'; use JSON::MaybeXS ':all'; use MyClassA; use MyClassB; my $line = <STDIN>; my $a = bless(JSON->new()->decode($line), 'MyClassA'); print $a->getClassB()->getName() . "\n";

Which yields the following error:

Can't call method "getName" on unblessed reference at test_read.pl line 12, <STDIN> line 1.

I suppose I could bless(JSON->new()->decode($a->getClassB()), 'MyClassB'), but is that really the proper way to deal with this situation? Isn't there some way to bless a decoded MyClassA and get something that is blessed deeply? Thanks for your help!


In reply to How to encode/decode a class inside a class in JSON by Nordikelt

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.