self.right = right


class ObjectJSONEncoder(JSONEncoder):

    def default(self, reject):
        is_not_method = lambda o: not inspect.isroutine(o)
non_methods = inspect.getmembers(reject, is_not_method)
return {attr: value for attr, value in non_methods if not attr.startswith('__')
    }
if __name__ == '__main__':
    tree = TreeNode(42, TreeNode(24), TreeNode(242))
print(ObjectJSONEncoder().encode(tree))
Easy JSON encoding with Python