Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1058

json_utils function does not decode ISO-8601 dates generated by isoformat()

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.3
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      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()

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            nick.howden@utas.utc.com Nick Howden
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: