I have a JSON string like
use JSON::XS qw(decode_json); say Dumper( decode_json($json) );
will produce:
$VAR1 = { 'Fname' => 'SomeFname', 'Lname' => 'SomeLname', 'Addr' => { 'Street => 'Somestreet', 'Zip' => '00000', }, };
I am looking for an easy way to "convert" a JSON string (or perl structure) into Perl / Moose objects, for example:
package My; use Moose; has 'Fname' => (is => 'rw', isa => 'Str'); has 'Lname' => (is => 'rw', isa => 'Str'); has 'Addr' => (is => 'rw', isa => 'My::Addr');
and
package My::Addr; use Moose; has 'Street' => (is => 'rw', isa => 'Str'); has 'Zip' => (is => 'rw', isa => 'Str');
The problem consists of two parts:
- defining a Moose class hierarchy based on a JSON string (once)
- initialization of object instances with real values ββfrom JSON (for each JSON)
I am not very skilled at Moose, so you need links to study this specific problem.
(Moose BIG - so reading everything in CPAN will certainly help, but this is too much to start. Therefore, I am looking for step-by-step training in a real problem - for example, above).
Main questions:
- Is it possible to generate Moose class determinants (perl source) from a data structure? Is there such a CPAN module?
- when I received the class hierarchy (for example, I can write them manually if there is no helper here), what is the easiest way to create (initialize) my instances from JSON?
source share