Skip to content

Instantly share code, notes, and snippets.

@ojacobson
Created December 4, 2012 18:56
Show Gist options
  • Save ojacobson/4207432 to your computer and use it in GitHub Desktop.
Save ojacobson/4207432 to your computer and use it in GitHub Desktop.
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'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment