Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Framework for making Moose-compatible object from json

by nataraj (Sexton)
on Aug 21, 2022 at 04:39 UTC ( [id://11146281]=note: print w/replies, xml ) Need Help??


in reply to Re: Framework for making Moose-compatible object from json
in thread Framework for making Moose-compatible object from json

Maybe what you need is just a set of rules which map parts of the structure to their corresponding class.

That's exactly what I need. I only did not managed to put it into words properly

There is a chance that Moose itself is all the "framework" you need

This looks like a good idea, but it would miss backward serialization (object->json). So you can do something like that:

my $obj= MyCoolRestObject::UserInfo->new($api->get("/path/to/proper/ +call")); $obj->phone1("+722233344555"); $api->put("path/to/put/call",$obj->as_json);

So as far as I can get, there is no such mapper for JSON, that would work out of box the same way MooseX::DOM works for XML. And may be I should write it first, and then use it

Replies are listed 'Best First'.
Re^3: Framework for making Moose-compatible object from json
by haj (Vicar) on Aug 21, 2022 at 10:42 UTC
    And may be I should write it first, and then use it

    There's nothing wrong with that approach, of course. On the other hand, object->JSON conversion isn't all that difficult, so I added it to my example (I also fixed a typo where I had user_name instead of username).

    In my example, the Moose class definition plays the role of the DOM, so you don't need to store class metadata in JSON. JSON does not know about classes, so you need to tell the JSON encoder what to do if it hits an object. To do this, I use the convert_blessed property of the JSON::PP encoder and define a TO_JSON method which just reduces an object to its underlying hash reference.

    A final note before we get to the code: I've been using JSON::PP because it's in Perl core. The code works just as fine with JSON which will use one of the faster XS implementations if they're available.

    use 5.028; use warnings; package MyCoolRestObject; use Moose; use Moose::Util::TypeConstraints; with 'MyCoolRestObject::Serializer'; has body_raw => ( is => 'ro' ); has url => ( is => 'ro' ); has datetime => ( is => 'ro' ); has entry_id => ( is => 'ro' ); has subject_html => ( is => 'ro' ); coerce 'MyCoolRestObject::Poster' => from 'HashRef' => via { MyCoolRestObject::Poster->new(%$_) }; has poster => ( is => 'ro', isa => 'MyCoolRestObject::Poster', coerce => 1, ); coerce 'MyCoolRestObject::Icon' => from 'HashRef' => via { MyCoolRestObject::Icon->new(%$_) }; has icon => ( is => 'ro', isa => 'MyCoolRestObject::Icon', coerce => 1, ); has subject_raw => ( is => 'ro' ); has body_html => ( is => 'ro' ); # -------------------------------------------------------------------- package MyCoolRestObject::Icon; use Moose; with 'MyCoolRestObject::Serializer'; has url => ( is => 'ro' ); has comment => ( is => 'ro' ); has picid => ( is => 'ro' ); has keywords => ( is => 'ro' ); has username => ( is => 'ro' ); # -------------------------------------------------------------------- package MyCoolRestObject::Poster; use Moose; with 'MyCoolRestObject::Serializer'; has display_name => ( is => 'ro' ); has username => ( is => 'ro' ); # -------------------------------------------------------------------- package MyCoolRestObject::Serializer; use Moose::Role; sub TO_JSON { +{ shift->%* } } # -------------------------------------------------------------------- package main; use Cpanel::JSON::XS; $/ = undef; my $json = decode_json <DATA>; # Cpanel::JSON::XS -> Object: my $obj = MyCoolRestObject->new(%$json); say $obj->icon->username; # Object->Cpanel::JSON::XS: my $encoder = Cpanel::JSON::XS->new->utf8->pretty->convert_blessed; my $json_out = $encoder->encode($obj); say $json_out; __DATA__ { "body_raw" : "This is <b>Entry 1</b>", "url" : "http://nataraj.nataraj.hack.dreamwidth.net/459.html", "datetime" : "2022-08-20 16:36:00", "entry_id" : 459, "subject_html" : "Entry 1", "poster" : { "display_name" : "nataraj", "username" : "nataraj" }, "icon" : { "url" : "http://www.nataraj.hack.dreamwidth.net/userpic/1/3", "comment" : "4", "picid" : 1, "keywords" : [ "3" ], "username" : "nataraj" }, "subject_raw" : "Entry 1", "body_html" : "This is <b>Entry 1</b>" }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146281]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found