def json_datetime_serial ( obj ) : if isinstance ( obj , ( datetime , date ) ) : serial = obj . isoformat ( ) return serial if ObjectId is not None and isinstance ( obj , ObjectId ) : # TODO: try to use bson.json_util instead return str ( obj ) raise TypeError ( "Type not serializable" )
python object with datetime to json
JSON serializer for objects not serializable by default json code
def json_datetime_serial(obj):
    """JSON serializer for objects not serializable by default json code"""
    if isinstance(obj, (datetime, date)):
        serial = obj.isoformat()
        return serial

    if ObjectId is not None and isinstance(obj, ObjectId):
        # TODO: try to use bson.json_util instead
        return str(obj)

    raise TypeError("Type not serializable")