Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Framework for making Moose-compatible object from json

by NERDVANA (Chaplain)
on Aug 20, 2022 at 14:17 UTC ( #11146259=note: print w/replies, xml ) Need Help??


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

If you mean that you have Moose classes/roles available and you want to detect incoming objects in your JSON and call  ->new on the appropriate class, maybe also calling apply_all_roles to customize the object a bit, then you can do that using the JSON module's filter_json_object feature.

On the other hand, if you mean that you want to create a brand new Moose class with one attribute for each of the keys seen in the incoming hash, and a metaclass that enumerates these attributes, you have a harder problem. The hard part of the problem isn't creating the Moose class; the hard part is that once you create a Moose class it remains in memory for the duration of the program, because classes are global. You would want to at least recognize when the set of attributes was the same as a previous class and re-use that class. But, a malicious client could connect to your service requesting infinite different attributes, and run you out of memory. A possible solution here would be to create the class with a DESTROY method that deletes the entire class when the one-and-only instance of that class goes out of scope.

An additional problem is that some names are reserved by Perl or Moose. So if an object came in looking like

{ new => 1, meta => 2, BUILDARGS => 3, DESTROY => 4 }
blindly creating attribute accessors for it would be bad. I think you could still access the attributes using the MOP, as long as you didn't create accessors.

So if you only access the attributes using the MOP, then you might actually have a path forward, because you could make a subclass of the MOP classes which return dynamic info about the instance instead of static info about the class. Then you only need one Moose class, and it generates dynamic MOP on demand when you call  ->meta.

Then, the final thing you need to ask yourself is whether all of this is actually solving your problem, or just creating more work :-)

Replies are listed 'Best First'.
Re^2: Framework for making Moose-compatible object from json
by nataraj (Sexton) on Aug 20, 2022 at 17:46 UTC
    for each of the keys seen in the incoming hash
    No, I would consider, that key list are known in advance, and Class hierarchy is predefined. But I guess I will need some framework to quickly map json keys to class attributes. See example I've posted in a comment above...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2023-09-26 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?