hltcoe/patapsco

patapsco-web server return TypeError: Object of type 'Doc' is not JSON serializable

Closed this issue · 3 comments

Hi patapsco is a so cooool repo I have seen!

when I try patapsco-web command, and request /doc/ path, the log show TypeError: Object of type 'Doc' is not JSON serializable ?

Any wrong with my expriments?

Thanks!

 * Running on http://0.0.0.0:9999/ (Press CTRL+C to quit)
[2022-09-15 18:11:56,677] ERROR in app: Exception on /doc/4998647 [GET]
Traceback (most recent call last):
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
    resp = make_response(f(*args, **kwargs))
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/patapsco/bin/web.py", line 45, in document
    return flask.jsonify(db[id])
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/json.py", line 263, in jsonify
    (dumps(data, indent=indent, separators=separators), '\n'),
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/json.py", line 123, in dumps
    rv = _json.dumps(obj, **kwargs)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/json/encoder.py", line 437, in _iterencode
    o = _default(o)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/json.py", line 80, in default
    return _json.JSONEncoder.default(self, o)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Doc' is not JSON serializable

and /query/ also show exception:

TypeError: Object of type 'Results' is not JSON serializable

[2022-09-15 18:16:13,665] ERROR in app: Exception on /query/man [GET]
Traceback (most recent call last):
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
    resp = make_response(f(*args, **kwargs))
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/patapsco/bin/web.py", line 52, in retrieve
    return flask.jsonify(retriever.process(query))
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/json.py", line 263, in jsonify
    (dumps(data, indent=indent, separators=separators), '\n'),
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/json.py", line 123, in dumps
    rv = _json.dumps(obj, **kwargs)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/json/encoder.py", line 437, in _iterencode
    o = _default(o)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/site-packages/flask/json.py", line 80, in default
    return _json.JSONEncoder.default(self, o)
  File "/home/zkli/miniconda3/envs/patapsco/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Results' is not JSON serializable
cash commented

Thanks for the report @lizekui. I haven't come across this error. In all my environments Flask has been able to encode the objects. What version of Flask are you running with?

wow! dear @cash , your solution is great for me!
I check my Flask version while it is too old (Flask 0.12.x)
After pip install -U Flask, Flask 2.0.3 is never encounter this Exception.

Thanks!