Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

A difference between a JSON data structure and a Perl object is that Perl objects are blessed to a class. In your other comment you write that the Class hierarchy is predefined. So it appears the actual task is to find the correct class for each of the references you find in your JSON data structure. But then, there's the phrase adds object specific methods, some internal logic which creates some friction. If the class hierarchy is predefined, then every object should be happy with the methods this class provides, and the user code should already contain the internal logic.

Maybe what you need is just a set of rules which map parts of the structure to their corresponding class. Users of MooseX::DOM create a DOM to define these rules for XML data. With MooseX::Role::JSONObject you can convert JSON data to objects if the JSON data itself contain the rules, which they do if the JSON data have been created by the very same module. That doesn't seem to be the case for your JSON data.

There is a chance that Moose itself is all the "framework" you need: If your classes are written as Moose-classes matching the JSON hashes, then you can just feed the JSON hash to the class constructor. The attribute declaration in the class can then use coercion to turn JSON hash references to objects if an attribute is supposed to be an object. Or, slightly more messy, you can use the BUILDARGS methods.

Here's some code, based on your JSON example:

use 5.028; use warnings; package MyCoolRestObject; use Moose; use Moose::Util::TypeConstraints; 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; 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; has display_name => ( is => 'ro' ); has user_name => ( is => 'ro' ); # -------------------------------------------------------------------- +-- package main; use JSON::PP; use Data::Dump qw/dump/; $/ = undef; my $json = decode_json <DATA>; my $obj = MyCoolRestObject->new(%$json); print $obj->icon->username; __DATA__ { "body_raw" : "This is <b>Entry 1</b>", "url" : "", "datetime" : "2022-08-20 16:36:00", "entry_id" : 459, "subject_html" : "Entry 1", "poster" : { "display_name" : "nataraj", "username" : "nataraj" }, "icon" : { "url" : "", "comment" : "4", "picid" : 1, "keywords" : [ "3" ], "username" : "nataraj" }, "subject_raw" : "Entry 1", "body_html" : "This is <b>Entry 1</b>" }

In reply to Re: Framework for making Moose-compatible object from json by haj
in thread Framework for making Moose-compatible object from json by nataraj

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2023-11-30 00:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found