Description
bson.json_util appears to only support a subset of the possible interpretations of the ISO-8601 format for $date.
Here is a "valid" ISO-8601 formatted string as returned by the mongo shell:
MongoDB shell version: 3.0.0
|
connecting to: test
|
> bah = new Date()
|
ISODate("2015-03-18T20:30:50.508Z")
|
> bah.toISOString()
|
2015-03-18T20:30:50.508Z
|
>
|
But this same string cannot be parsed by bson.json_util.loads:
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
|
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
|
Type "help", "copyright", "credits" or "license" for more information.
|
>>> import pymongo
|
>>> pymongo.version
|
'2.8'
|
>>> import bson.json_util
|
>>> bson.json_util.loads('{"$date":"2015-03-18T20:30:50.508Z"}')
|
Traceback (most recent call last):
|
File "<stdin>", line 1, in <module>
|
File "/Library/Python/2.7/site-packages/bson/json_util.py", line 158, in loads
|
return json.loads(s, *args, **kwargs)
|
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 351, in loads
|
return cls(encoding=encoding, **kw).decode(s)
|
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
|
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
|
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
|
obj, end = self.scan_once(s, idx)
|
File "/Library/Python/2.7/site-packages/bson/json_util.py", line 157, in <lambda>
|
kwargs['object_hook'] = lambda dct: object_hook(dct, compile_re)
|
File "/Library/Python/2.7/site-packages/bson/json_util.py", line 204, in object_hook
|
raise ValueError("invalid format for offset")
|
ValueError: invalid format for offset
|
>>>
|
I get around this locally by replacing https://github.com/mongodb/mongo-python-driver/blob/master/bson/json_util.py#L151 with:
if not offset or offset == "Z":
|
However, there are other interpretations of ISO-8601 that should be parseable as well. For example, the following should all be parsed the same:
- 2015-03-18T20:30:50.508Z
- 2015-03-18T20:30:50.508+0000
- 2015-03-18T20:30:50.508+00:00