Skip to content

PYTHON-2354 Add support for JSONOptions.with_options #482

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions bson/codec_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,9 @@ def with_options(self, **kwargs):

.. versionadded:: 3.5
"""
return CodecOptions(
kwargs.get('document_class', self.document_class),
kwargs.get('tz_aware', self.tz_aware),
kwargs.get('uuid_representation', self.uuid_representation),
kwargs.get('unicode_decode_error_handler',
self.unicode_decode_error_handler),
kwargs.get('tzinfo', self.tzinfo),
kwargs.get('type_registry', self.type_registry)
)
opts = self._asdict()
opts.update(kwargs)
return CodecOptions(**opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!



DEFAULT_CODEC_OPTIONS = CodecOptions(
Expand Down
20 changes: 20 additions & 0 deletions bson/json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,26 @@ def _arguments_repr(self):
self.json_mode,
super(JSONOptions, self)._arguments_repr()))

def with_options(self, **kwargs):
"""
Make a copy of this JSONOptions, overriding some options::

>>> from bson.json_util import CANONICAL_JSON_OPTIONS
>>> CANONICAL_JSON_OPTIONS.tz_aware
True
>>> json_options = CANONICAL_JSON_OPTIONS.with_options(tz_aware=False)
>>> json_options.tz_aware
False

.. versionadded:: 3.12
"""
opts = self._asdict()
for opt in ('strict_number_long', 'datetime_representation',
'strict_uuid', 'json_mode'):
opts[opt] = kwargs.get(opt, getattr(self, opt))
opts.update(kwargs)
return JSONOptions(**opts)


LEGACY_JSON_OPTIONS = JSONOptions(json_mode=JSONMode.LEGACY)
""":class:`JSONOptions` for encoding to PyMongo's legacy JSON format.
Expand Down
28 changes: 28 additions & 0 deletions test/test_json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ def round_trip(self, doc, **kwargs):
def test_basic(self):
self.round_trip({"hello": "world"})

def test_json_options_with_options(self):
opts = json_util.JSONOptions(
datetime_representation=DatetimeRepresentation.NUMBERLONG)
self.assertEqual(
opts.datetime_representation, DatetimeRepresentation.NUMBERLONG)
opts2 = opts.with_options(
datetime_representation=DatetimeRepresentation.ISO8601)
self.assertEqual(
opts2.datetime_representation, DatetimeRepresentation.ISO8601)

opts = json_util.JSONOptions(strict_number_long=True)
self.assertEqual(opts.strict_number_long, True)
opts2 = opts.with_options(strict_number_long=False)
self.assertEqual(opts2.strict_number_long, False)

opts = json_util.CANONICAL_JSON_OPTIONS
self.assertNotEqual(
opts.uuid_representation, UuidRepresentation.JAVA_LEGACY)
opts2 = opts.with_options(
uuid_representation=UuidRepresentation.JAVA_LEGACY)
self.assertEqual(
opts2.uuid_representation, UuidRepresentation.JAVA_LEGACY)
self.assertEqual(opts2.document_class, dict)
opts3 = opts2.with_options(document_class=SON)
self.assertEqual(
opts3.uuid_representation, UuidRepresentation.JAVA_LEGACY)
self.assertEqual(opts3.document_class, SON)

def test_objectid(self):
self.round_trip({"id": ObjectId()})

Expand Down