-
Type:
Improvement
-
Resolution: Done
-
Priority:
Minor - P4
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
We have a json encoder which overrides the default encoder so that timestamps are encoded as ISO-8601 strings ...
def our_default_encoder(obj):
"""Override the json encoder that comes with pymongo (bson)
So that datetime objects are encoded as ISO-8601"""
if isinstance(obj, datetime.datetime):
iso = obj.isoformat()
return {"$date": iso}
return json_util.default(obj)
Even though the bson decoder in json_util parses some ISO-8601 strings it assumes that the seconds field contains 3 decimal places (i.e. milliseconds)
Could the object hook be augmented to parse a wider range of ISO-8601 timestamps? Possibly by ...
import iso8601
def object_hook(dct):
...
if "$date" in dct:
dtm = dct["$date"]
if isinstance(dtm, string_type):
dt = iso8601.parse_date(dtm)
return dt
...
I have tested this against the current set of unittests and it appears to maintain the current behaviour and will parse strings generated by isoformat()