|
17 | 17 | This module provides two helper methods `dumps` and `loads` that wrap the
|
18 | 18 | native :mod:`json` methods and provide explicit BSON conversion to and from
|
19 | 19 | JSON. :class:`~bson.json_util.JSONOptions` provides a way to control how JSON
|
20 |
| -is emitted and parsed, with the default being the legacy PyMongo format. |
21 |
| -:mod:`~bson.json_util` can also generate Canonical or Relaxed `Extended JSON`_ |
22 |
| -when :const:`CANONICAL_JSON_OPTIONS` or :const:`RELAXED_JSON_OPTIONS` is |
| 20 | +is emitted and parsed, with the default being the Relaxed Extended JSON format. |
| 21 | +:mod:`~bson.json_util` can also generate Canonical or legacy `Extended JSON`_ |
| 22 | +when :const:`CANONICAL_JSON_OPTIONS` or :const:`LEGACY_JSON_OPTIONS` is |
23 | 23 | provided, respectively.
|
24 | 24 |
|
25 | 25 | .. _Extended JSON: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst
|
|
32 | 32 | >>> loads('[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": "80", "$binary": "AQIDBA=="}}]')
|
33 | 33 | [{'foo': [1, 2]}, {'bar': {'hello': 'world'}}, {'code': Code('function x() { return 1; }', {})}, {'bin': Binary(b'...', 128)}]
|
34 | 34 |
|
35 |
| -Example usage (serialization): |
| 35 | +Example usage with :const:`RELAXED_JSON_OPTIONS` (the default): |
36 | 36 |
|
37 | 37 | .. doctest::
|
38 | 38 |
|
39 | 39 | >>> from bson import Binary, Code
|
40 | 40 | >>> from bson.json_util import dumps
|
41 | 41 | >>> dumps([{'foo': [1, 2]},
|
42 | 42 | ... {'bar': {'hello': 'world'}},
|
43 |
| - ... {'code': Code("function x() { return 1; }", {})}, |
| 43 | + ... {'code': Code("function x() { return 1; }")}, |
44 | 44 | ... {'bin': Binary(b"\x01\x02\x03\x04")}])
|
45 |
| - '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }", "$scope": {}}}, {"bin": {"$binary": "AQIDBA==", "$type": "00"}}]' |
| 45 | + '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]' |
46 | 46 |
|
47 | 47 | Example usage (with :const:`CANONICAL_JSON_OPTIONS`):
|
48 | 48 |
|
|
57 | 57 | ... json_options=CANONICAL_JSON_OPTIONS)
|
58 | 58 | '[{"foo": [{"$numberInt": "1"}, {"$numberInt": "2"}]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]'
|
59 | 59 |
|
60 |
| -Example usage (with :const:`RELAXED_JSON_OPTIONS`): |
| 60 | +Example usage (with :const:`LEGACY_JSON_OPTIONS`): |
61 | 61 |
|
62 | 62 | .. doctest::
|
63 | 63 |
|
64 | 64 | >>> from bson import Binary, Code
|
65 |
| - >>> from bson.json_util import dumps, RELAXED_JSON_OPTIONS |
| 65 | + >>> from bson.json_util import dumps, LEGACY_JSON_OPTIONS |
66 | 66 | >>> dumps([{'foo': [1, 2]},
|
67 | 67 | ... {'bar': {'hello': 'world'}},
|
68 |
| - ... {'code': Code("function x() { return 1; }")}, |
| 68 | + ... {'code': Code("function x() { return 1; }", {})}, |
69 | 69 | ... {'bin': Binary(b"\x01\x02\x03\x04")}],
|
70 |
| - ... json_options=RELAXED_JSON_OPTIONS) |
71 |
| - '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]' |
| 70 | + ... json_options=LEGACY_JSON_OPTIONS) |
| 71 | + '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }", "$scope": {}}}, {"bin": {"$binary": "AQIDBA==", "$type": "00"}}]' |
72 | 72 |
|
73 | 73 | Alternatively, you can manually pass the `default` to :func:`json.dumps`.
|
74 | 74 | It won't handle :class:`~bson.binary.Binary` and :class:`~bson.code.Code`
|
|
0 commit comments