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

json_util.loads does not decode $binary type 0 into bytes in Python 3

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.5
    • Affects Version/s: None
    • Component/s: JSON
    • None
    • Minor Change

      In json_util with Python 3 we encode bytes to "$binary" but we do not decode "$binary" back into bytes:

      >>> from bson.json_util import loads, dumps
      >>> dumps(b'')
      '{"$binary": "", "$type": "00"}'
      >>> loads('{"$binary": "", "$type": "00"}')
      Binary(b'', 0)
      

      The correct behavior in Python 3 should be:

      >>> loads('{"$binary": "", "$type": "00"}')
      b''
      

      This would match how the bson module handles bytes. This is a minor backward breaking change for Python 3 users. Code that relies on "$binary" always being decoded into a bson.Binary will break. Code that relies on "$binary" being decoded into bytes should be unaffected as bson.Binary is already a subclass of bytes, eg this code works on Python 2 and 3 before and after this change:

      >>> binary = loads('{"$binary": "", "$type": "00"}')
      >>> assert isinstance(binary, bytes)
      

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: