pysolr
pysolr copied to clipboard
Solr.add with datetime instance causes TypeError
Calling pysolr.Solr.add with a dict containing a datetime object causes the following stack trace:
File "<>/.local/lib/python3.7/site-packages/pysolr.py", line 1018, in add
m = json.dumps(message).encode("utf-8")
File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable
I believe this change to be the breaking change.
Before this change the datetime object could propagate via self._build_doc to self._from_python where the first check would have converted it to a string.
I have
- [x] Tested with the latest release
- [ ] Tested with the current master branch
- [x] Searched for similar existing issues
Expected behaviour
No exception
Actual behaviour
TypeError: Object of type datetime is not JSON serializable
Configuration
- Operating system version: debian 10
- Search engine version: 8.5.1
- Python version: 3.7
- pysolr version: 3.9.0
I see there was no reply yet to this issue. Is there any plan to solve it or at least any suggested workaround? Thx
You can go back to using the XML interface using the optional parameter, call isoformat() on the date objects before passing them in. It would be nice if we had a JSON encoder which handles this, too, if you have time to contribute one.
Thanks for the suggestion.
I am now just passing datetime(...).isoformat() instead of datetime(...) and everything works fine. I did not try the XML interface.
Regarding the encoder, I will not have the time, unfortunately.