Skip to content

Function to jsonify BSON objects #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

Dannnno
Copy link

@Dannnno Dannnno commented Sep 11, 2015

  • Add function to jsonify BSON objects
  • Add test

closes #62

- Add function to jsonify BSON objects
- Add test

Issue mongodb-labs#62
@Dannnno Dannnno changed the title Function to jsonify BSON objects WIP: Function to jsonify BSON objects Sep 12, 2015
- Fix functionality
- Add many more tests

Issue: 62
@Dannnno Dannnno changed the title WIP: Function to jsonify BSON objects Function to jsonify BSON objects Sep 12, 2015
- remove unused import
- Fix name of function in __all__

Issue: 62
@dcrosta
Copy link
Contributor

dcrosta commented May 22, 2017

I don't think we want to be dumping to JSON, parsing from JSON, then dumping again. @ajdavis do you know of anything better to do here?

I think at worst we can use the JSON string returned by json_utils and create a Flask response out of that directly. Flask's jsonify doesn't do a whole lot more than that anyway).

@ajdavis
Copy link

ajdavis commented May 22, 2017

I don't understand the purpose of the BSON-encoding step at all. If you look at the json_util documentation it shows:

>>> from bson import Binary, Code
>>> from bson.json_util import dumps
>>> dumps([{'foo': [1, 2]},
...        {'bar': {'hello': 'world'}},
...        {'code': Code("function x() { return 1; }", {})},
...        {'bin': Binary(b"\x01\x02\x03\x04")}])
'[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }", "$scope": {}}}, {"bin": {"$binary": "AQIDBA==", "$type": "00"}}]'

So I think that calling json_util.dumps directly on the input obj creates the string you want. The only additional work that Flask's jsonify does is apply some styling defaults and wrap it in a Response, you could do that in flask_pymongo.jsonify, too.

@dcrosta
Copy link
Contributor

dcrosta commented May 22, 2017

Yeah, that's kind of what I thought as well. Leaving this for 2.0, in any case.

@dcrosta dcrosta added this to the 2.0 milestone May 22, 2017
@dcrosta
Copy link
Contributor

dcrosta commented Jun 4, 2019

Closing in favor of #122

@dcrosta dcrosta closed this Jun 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jsonify compatibility
3 participants