Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-1335

libbson does not properly serialize the Code type to JSON

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.5.0
    • Affects Version/s: None
    • Component/s: json, libbson
    • Labels:
    • Minor Change

      The libsson function bson_as_json does not properly serialize the extended JSON type Code.

      The expected behavior is to output an object with "$code" and "$scope" attributes as demonstrated by json_util from PyMongo

      >>> import bson
      >>> from bson import json_util
      >>> from bson.code import Code
      >>> json_util.dumps({"code": Code("function x() { return 1; }")})
      '{"code": {"$code": "function x() { return 1; }", "$scope": {}}}'
      >>> b = bson.BSON.encode({"code": Code("function x() { return 1; }")})
      >>> b
      '*\x00\x00\x00\rcode\x00\x1b\x00\x00\x00function x() { return 1; }\x00\x00'
      >>> import bjson
      >>> bjson.dumps(b)
      '{ "code" : "function x() { return 1; }" }'
      >>>
      

      bjson.dumps is a thin wrapper around bson_as_json using the Python C API.

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: