Question

We have a REST web service written in Perl Dancer. It returns perl data structures in YAML format and also takes in parameters in YAML format - it is supposed to work with some other teams who query it using Python.

Here's the problem -- if I'm passing back just a regular old perl hash by Dancer's serialization everything works completely fine. JSON, YAML, XML... they all do the job.

HOWEVER, sometimes we need to pass Perl objects back that the Python can later pass back in as a parameter to help with unnecessary loading, etc. I played around and found that YAML is the only one that works with Perl's blessed objects in Dancer.

The problem is that Python's YAML can't parse through the YAMLs of the Perl objects (whereas it can handle regular old perl hash YAMLs without an issue).

The perl objects start out like this in YAML:

First one:

--- &1 !!perl/hash:Sequencing_API

Second:

--- !!perl/hash:SDB::DBIO

It errors out like this.

yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:perl/hash:SDB::DBIO'

The regular files seem to get passed through like this:

--- fields: library:

It seems like the extra stuff after --- are causing the issues. What can I do to address this? Or am I trying to do too much by passing around Perl objects?

Was it helpful?

Solution

the short answer is

!! is yaml shorthand for tag:yaml.org,2002: ... as such !!perl/hash is really tag:yaml.org,2002:perl/hash

now you need to tell python yaml how to deal with this type

so you add a constructor for it as follows

import yaml


def construct_perl_object(loader, node):
    print "S:",suffix,"N:",node
    return loader.construct_yaml_node(node)#this is likely wrong ....



yaml.add_multi_constructor(u"tag:yaml.org,2002:perl/hash:SDB::DBIO", construct_perl_object)
yaml.load(yaml_string)

or maybe just parse it out or return None maybe ... its hard to test with just that line ... but that may be what you are looking for

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top