I would like to serialize a custom class (which I cannot modify or monkey patch) by specifying how such class instances should be serialized.
Here's the setup:
# some custom class which I cannot modify
class Custom:
def __init__(self, a, b):
self.a = a
self.b = b
# data I want to serialize
data = [Custom(1, 2), Custom(101, 102)]
Here's how I would do it for JSON:
import json
# helper function to handle the custom class
def default(d):
if isinstance(d, Custom):
return dict(a=d.a, b=d.b)
print(json.dumps(data, default=default))
# expected and actual output: [{"a": 1, "b": 2}, {"a": 101, "b": 102}]
I'm struggling to find an equivalent solution for pyyaml
:
import yaml
def yaml_equivalent_of_default():
"YOUR SOLUTION GOES HERE"
print(yaml.dump(data))
# expected output:
# - a: 1
# b: 2
# - a: 101
# b: 102
I've tried different approaches mentioned in the pyyaml docs to no avail.