json_util.default should always convert Int64

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Unknown
    • 4.7
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Context

      orjson is a fast JSON encoder that could be used to speed up encoding and decoding MongoDB Extended JSON, like this:

      >>> doc = {'a': 1, 'b': 'str', 'c': [], 'd': {}, 'e': tuple(), 'f': 1.1, 'g': float('nan'), 'h': Code('str')}
      >>> orjson.dumps(doc, option=orjson.OPT_PASSTHROUGH_SUBCLASS, default=json_util.default)
      b'{"a":1,"b":"str","c":[],"d":{},"e":[],"f":1.1,"g":null,"h":{"$code":"str"}}'
      >>> json_util.dumps(doc)
      '{"a": 1, "b": "str", "c": [], "d": {}, "e": [], "f": 1.1, "g": {"$numberDouble": "NaN"}, "h": {"$code": "str"}}'
      

      Note that there are some slight differences in output which unfortunately means PyMongo can't use orjson by default. For example, orjson encodes nan as null which violates the Extended JSON spec.

      However, this code breaks on Int64 values because json_util.default is incompatible with orjson w/ OPT_PASSTHROUGH_SUBCLASS due to Int64:

      >>> orjson.dumps({'a': Int64(1)})
      b'{"a":1}'
      >>> orjson.dumps({'a': Int64(1)}, default=json_util.default)
      b'{"a":1}'
      >>> orjson.dumps({'a': Int64(1)}, default=json_util.default, option=orjson.OPT_PASSTHROUGH_SUBCLASS)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: default serializer exceeds recursion limit
      

      Definition of done

      When encoding an Int64 in relaxed mode (the default), json_util.default should convert Int64 to a standard int so that orjson can encode it.

            Assignee:
            Shane Harvey
            Reporter:
            Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: