Skip to content
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

Use a default serializer that is not only for date types #664

Merged
merged 1 commit into from
Jul 9, 2021
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
17 changes: 15 additions & 2 deletions jupyter_client/jsonutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Distributed under the terms of the Modified BSD License.
import re
import warnings
from binascii import b2a_base64
from datetime import datetime
from typing import Optional
from typing import Union
Expand Down Expand Up @@ -91,9 +92,21 @@ def squash_dates(obj):


def date_default(obj):
"""default function for packing datetime objects in JSON."""
"""DEPRECATED: Use jupyter_client.jsonutil.json_default"""
warnings.warn(
"date_default is deprecated since jupyter_client 7.0.0."
" Use jupyter_client.jsonutil.json_default.",
stacklevel=2,
)
return json_default(obj)


def json_default(obj):
"""default function for packing objects in JSON."""
if isinstance(obj, datetime):
obj = _ensure_tzinfo(obj)
return obj.isoformat().replace("+00:00", "Z")
return obj.isoformat().replace('+00:00', 'Z')
elif isinstance(obj, bytes):
return b2a_base64(obj).decode('ascii')
else:
raise TypeError("%r is not JSON serializable" % obj)
4 changes: 2 additions & 2 deletions jupyter_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

from jupyter_client import protocol_version
from jupyter_client.adapter import adapt
from jupyter_client.jsonutil import date_default
from jupyter_client.jsonutil import extract_dates
from jupyter_client.jsonutil import json_default
from jupyter_client.jsonutil import squash_dates


Expand Down Expand Up @@ -94,7 +94,7 @@ def squash_unicode(obj):
def json_packer(obj):
return jsonapi.dumps(
obj,
default=date_default,
default=json_default,
ensure_ascii=False,
allow_nan=False,
)
Expand Down
4 changes: 2 additions & 2 deletions jupyter_client/tests/test_jsonutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def test_parse_ms_precision():
assert isinstance(parsed, str)


def test_date_default():
def test_json_default():
naive = datetime.datetime.now()
local = tzoffset("Local", -8 * 3600)
other = tzoffset("Other", 2 * 3600)
data = dict(naive=naive, utc=utcnow(), withtz=naive.replace(tzinfo=other))
with mock.patch.object(jsonutil, "tzlocal", lambda: local):
with pytest.deprecated_call(match="Please add timezone info"):
jsondata = json.dumps(data, default=jsonutil.date_default)
jsondata = json.dumps(data, default=jsonutil.json_default)
assert "Z" in jsondata
assert jsondata.count("Z") == 1
extracted = jsonutil.extract_dates(json.loads(jsondata))
Expand Down