This is called coercion or unmarshalling. Using Moo + Types::Standard makes it pretty easy...

use v5.12; use strict; use warnings; package ToJson { use Moo::Role; sub TO_JSON { my $self = shift; return { %$self }; } } package Named { use Moo::Role; use Types::Standard -types; has NAME => ( is => 'bare', isa => Str, reader => 'getName', ); } package MyClassA { use Moo; use Types::Standard -types; with 'ToJson', 'Named'; has MY_CLASS_B => ( is => 'bare', reader => 'getClassB', writer => 'setClassB', isa => InstanceOf->of('MyClassB')->plus_constructors( Has +hRef, 'new' ), coerce => 1, ); } package MyClassB { use Moo; with 'ToJson', 'Named'; } use JSON::MaybeXS; my $a = MyClassA->new(NAME => 'A instance'); my $b = MyClassB->new(NAME => 'B instance'); $a->setClassB($b); my $json = JSON->new->convert_blessed->encode($a); say $json; my $inflated = MyClassA->new( JSON->new->decode($json) ); say $inflated->getClassB->getName;

The key part is this:

isa => InstanceOf->of('MyClassB')->plus_constructors( Has +hRef, 'new' ), coerce => 1,

This means that when the MY_CLASS_B attribute gets passed a hashref instead of an instance of MyClassB, it should call MyClassB->new on that hashref. This coercion happens in both the constructor (new) and the attribute writer (setClassB).

Of course, it can be done without Moo or Types::Standard. They're not magic. To do it manually, alter your MyClassA::new to check %extras to find if there's any values that need inflating. Something like this:

if ( exists $extras{MY_CLASS_B} and ref $extras{MY_CLASS_B} eq 'HA +SH' ) { $extras{MY_CLASS_B} = MyClassB->new( %{ $extras{MY_CLASS_B} } +); }

And probably a good idea to do something similar in setClassB. Allow it to accept a hashref and inflate it to a MyClassB object.


In reply to Re: How to encode/decode a class inside a class in JSON by tobyink
in thread 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.