def encode_model(obj, recursive=False):
    if obj is None:
        return obj

    import bson
    import datetime

    if isinstance(obj, (int, float, basestring)):
        out = obj
    elif isinstance(obj, list):
        out = [encode_model(item) for item in obj]
    elif isinstance(obj, dict):
        out = dict([(k, encode_model(v)) for (k, v) in obj.items()])
    elif isinstance(obj, (datetime.datetime, datetime.timedelta)):
        out = str(obj)
    elif isinstance(obj, bson.objectid.ObjectId):
        out = {'ObjectId': str(obj)}
    else:
        raise NameError("Could not JSON-encode type '%s': %s" % (type(obj), str(obj)))

    return out
dataset/ETHPy150Open joaoqalves/web2py-oauth2/models/utils.py/encode_model/original