Pathologically Eclectic Rubbish Lister | |
PerlMonks |
How to encode/decode a class inside a class in JSONby Nordikelt (Novice) |
on Aug 20, 2020 at 01:32 UTC ( [id://11120920]=perlquestion: print w/replies, xml ) | Need Help?? |
Nordikelt has asked for the wisdom of the Perl Monks concerning the following question: Here is a simple example of what I am attempting. I start with with MyClassA which will contain an instance of MyClassB:
and ,
Next, we build and encode MyClassA:
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:
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!
Back to
Seekers of Perl Wisdom
|
|