Discussion:
[mongodb-user] how to write the ObjectId object in yaml file.
Peng Dai
2016-01-12 06:35:12 UTC
Permalink
when write the unit test for my web API. I usually need to init some db
data.
I choose the YAML other than JSON, cause it is much easy to read and write.

the problem is I cannot write the ObjectId by hand.
If I use the ```yaml.dump``` to generate the result. it would looks like:
sku: !!python/object/new:bson.objectid.ObjectId
state: !!binary |
VZ3yU14L3F0zUWxT


this is really hard to me, I check the yaml help and some blog. It seems I
can write it like this:

sku: !!python/object/new:bson.objectid.ObjectId [
'5694992601f5b45ecebfeacb']
sku_seoncd: !!python/object/new:bson.objectid.ObjectId
oid: '5694992601f5b45ecebfeacb'

but both the two style not work at all. So what's the right way to write
the ObjectId object in a readable way to the yaml file?
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/6efb62c3-f9d2-4ecc-b681-cd183caf261f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Wan Bachtiar
2016-01-13 07:25:39 UTC
Permalink
Hi Peng Dai,

Using pyyaml <http://pyyaml.org/wiki/PyYAMLDocumentation> you can define
custom tags. You can register custom constructors and representers
<http://pyyaml.org/wiki/PyYAMLDocumentation#Constructorsrepresentersresolvers>
using the functions yaml.add_constructor and yaml.add_representer.

As an example, given Python dictionary which contain an ObjectId()
<https://docs.mongodb.org/manual/reference/object-id/> object like below:

{ "_id" : ObjectId("5695d388f11a8aea9fd322b7"),
"a" : 1, "b" : 1, "c" : 1 }

First you define a representer that converts an ObjectId() to a scalar
node, then register it using yaml.add_representer:

def objectid_representer(dumper, data):
return dumper.represent_scalar("!bson.objectid.ObjectId", str(data))

yaml.SafeDumper.add_representer(objectid.ObjectId, objectid_representer)
yaml.safe_dump(doc_with_objectid, yamlfile, default_flow_style=False)

The resulting yaml file content would look like :

_id: !bson.objectid.ObjectId '5695d388f11a8aea9fd322b7'
a: 1.0
b: 1.0
c: 1.0

To load it back in as an ObjectId(), register a constructor for the tag:

def objectid_constructor(loader, data):
return objectid.ObjectId(loader.construct_scalar(data))

yaml.add_constructor('!bson.objectid.ObjectId', objectid_constructor)
loaded = yaml.load(open(yamlfile, 'r'))

For the full example script see github gist: ObjectID_YAML.py
<https://gist.github.com/sindbach/fd3eb52747f467576f16>.

Regards,

Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/ee1e0b23-9961-4ab9-8318-9eefdcd42670%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...