You can, additionally, overlay values or computed values onto the JSON
representation by passing the optional `overlay` parameter as a dictionary
of callables:
>>> @jsonizable(atoms=['name'])
... class Person(object):
... def __init__(self, name):
... self.name = name
...
>>> @jsonizable(lists=['students'], objects=['teacher'])
... class Classroom(object):
... def __init__(self, students, teacher):
... self.students = students
... self.teacher = teacher
...
>>> students = [Person('bob')]
>>> teacher = Person('clarice')
>>> classroom = Classroom(students, teacher)
>>> classroom.to_json(overlay=dict(
... address=lambda classroom: '1 Infinite Loop',
... students=dict(
... hat_size=lambda student: 'Large'
... )
... ))
{'students': [{'name': 'bob', 'hat_size': 'Large'}], 'teacher': {'name': 'clarice'}, 'address': '1 Infinite Loop'}
Created
December 4, 2012 18:56
-
-
Save ojacobson/4207432 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment