Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by nataraj (Sexton)
on Aug 20, 2022 at 17:37 UTC ( #11146266=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

Generally I wonder, I am not the first one who met json<->moose mapping problem. There should be some existing solutions, and I should look at them and choose one that fits me better.

But if I imagine some ideal solution for me it would be something like that:

{ "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>" }
package Moose::MyCoolRestObject; use MooseX::JSONBase; has_json_string url => (); has_json_datetime datetime => (); has_json_int id => (); has_json_object icon => ("Moose::MyCoolRestObject::Icon");
And then use it like this:
my $obj = Moose::MyCoolRestObject->new($json); print $obj->icon->username;
I guess I can write something like this, if nothing similar does not exist...

Replies are listed 'Best First'.
Re^3: Framework for making Moose-compatible object from json (serialization)
by LanX (Saint) on Aug 20, 2022 at 19:01 UTC
    > who met json<->moose mapping problem

    The bidirectional mapping looks like you are interested in an "object serialization".

    NB: Then the JSON structure will originally derive from serializing your OOP system. °

    HTH narrowing your search! :)

    EDIT

    e.g. MooseX::Storage came up after a quick search.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    °) Your question was originally worded the other way round.

    > OP > to create Moose-compatible object out of perl-hash that came from json parser.

Re^3: Framework for making Moose-compatible object from json
by NERDVANA (Chaplain) on Aug 20, 2022 at 23:33 UTC
    has_json_string url => (); has_json_datetime datetime => ();
    What is the significance of "has_json_datetime"? How is that different from
    has datetime => ( is => 'rw', isa => 'DateTime', coerce => 1 );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2023-09-26 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?