code: 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")
docs: JSON serializer for objects not serializable by default json code