http://qs321.pair.com?node_id=11146257


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

Best you provide an SSCCE of what you want to achieve.

Moose is compatible to use plain Perl classes so I'm not sure what the requirements are.

The devil is in the details of the actual JSON semantics and what

> adds object specific methods, some internal logic, processing" etc

is supposed to mean.

edit

normally a converted JSON structure consists of a tree of nested Array's and Hashes and probably blessed Booleans.

Please be more specific.

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

  • Comment on Re: Framework for making Moose-compatible object from json

Replies are listed 'Best First'.
Re^2: Framework for making Moose-compatible object from json
by nataraj (Sexton) on Aug 20, 2022 at 17:37 UTC

    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...
      > 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.

      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 );