2

How do I convert a blessed object into JSON in Perl? Following is the array I have:

@x = ({
          'notificationtype' => 'TRAP',
          'receivedfrom' => 'UDP: [10.127.34.212]:48909->[10.127.34.182]:162',
          'version' => 1,
        },
        [
          [
            bless( {
                     'oidptr' => bless( do{\(my $o = '140059234062224)}, ''netsnmp_oidPtr' )
                   }, 'NetSNMP::OID' ),
            '600',
            67
          ],
          [
            bless( {
                     'oidptr' => bless( do{\(my $o = '140059236784112)}, ''netsnmp_oidPtr' )
                   }, 'NetSNMP::OID' ),
            'OID: .iso.org.dod.internet.private.enterprises.14296.1.100.0.0.1',
            6
          ]
]);

I am able to convert $VAR1 alone using encode_json \@x, but when I the use the blessed object, it's not working. I am getting the error:

encountered object 'NetSNMP::OID=HASH(0x136b278)', but neither allow_blessed nor convert_blessed settings are enabled at u.pl line 256.

I expected a serialized JSON object so that I can send it over to server.

How can I do it?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
  • 3
    What do you mean by "its not working"? – choroba Apr 27 '17 at 10:04
  • Also, what output do you expect? – choroba Apr 27 '17 at 10:09
  • i am getting the error: encountered object 'NetSNMP::OID=HASH(0x136b278)', but neither allow_blessed nor convert_blessed settings are enabled at u.pl line 256. – singamala sairam mohanreddy Apr 27 '17 at 10:09
  • i expected a serialized JSON object so that i can send it over to server. – singamala sairam mohanreddy Apr 27 '17 at 10:10
  • 2
    How should JSON serialize a Perl object of class `netsnmp_oidPtr` or `NetSNMP::OID`? – choroba Apr 27 '17 at 10:14
  • well i am not sure to be frank. but my requirement is to somehow serialize this so that i can send it over to rabbitmq server as i can't send it directly as an array. any options i may have to meet my req? – singamala sairam mohanreddy Apr 27 '17 at 10:17
  • 1
    You can't directly serialise blessed objects in JSON as it has no standard way of storing the class of a data structure. You could add another element to a hash-based object that stored the class name, but that doesn't allow for the less common classes that are based on other data types. If you don't have a requirement to use JSON then you could use something less portable like `FreezeThaw` or `Storable`. You should read [*Object Serialization Basics*](http://www.perlmonks.org/?node_id=8070) on PerlMonks. – Borodin Apr 27 '17 at 12:26
  • That's an inside-out Object, isn't it? Those are weird anyway. – simbabque Apr 27 '17 at 12:29
  • @Borodin that should be an answer. :) – Kev Apr 27 '17 at 14:02

1 Answers1

0

It depends, you want to preserve the visuals ? or the data and hierarchy ? same positions could not be rendered in other terminal sizes. I would preserve just the data and hierarchy. Probably I would diregard focus and events. I would start umarating the properties like the following and then creaginf a factory These are the ones I found for Box

container.options
container.position, 
container.getLines()
container.getText()
container.getContent
container.children
container.parent
container.style
container.type
container.visible
container.height
container.hidden
container.index

subclasses have rows, data, lines, and more... I dont think there is a mechanic way of doing it...

if you want to recreate the hierarchy, you also need to iterate on children and parent, probaly the entire tree.

Thinking about it perhaps just with this.options is enough and children and parent.. good luck will try the same...

cancerbero
  • 5,662
  • 1
  • 26
  • 18