Skip to content

Commit 1363f53

Browse files
committed
Use a default serializer that is not only for date types
This would allow making more general changes here. I am also adding serialization of bytes type into base64.
1 parent 42a19da commit 1363f53

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

jupyter_client/jsonutil.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Distributed under the terms of the Modified BSD License.
44
import re
55
import warnings
6+
from binascii import b2a_base64
67
from datetime import datetime
78
from typing import Optional
89
from typing import Union
@@ -91,9 +92,21 @@ def squash_dates(obj):
9192

9293

9394
def date_default(obj):
94-
"""default function for packing datetime objects in JSON."""
95+
"""DEPRECATED: Use jupyter_client.jsonutil.json_default"""
96+
warnings.warn(
97+
"date_default is deprecated since jupyter_client 7.0.0."
98+
" Use jupyter_client.jsonutil.json_default.",
99+
stacklevel=2,
100+
)
101+
return json_default(obj)
102+
103+
104+
def json_default(obj):
105+
"""default function for packing objects in JSON."""
95106
if isinstance(obj, datetime):
96107
obj = _ensure_tzinfo(obj)
97-
return obj.isoformat().replace("+00:00", "Z")
108+
return obj.isoformat().replace('+00:00', 'Z')
109+
elif isinstance(obj, bytes):
110+
return b2a_base64(obj).decode('ascii')
98111
else:
99112
raise TypeError("%r is not JSON serializable" % obj)

jupyter_client/session.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949

5050
from jupyter_client import protocol_version
5151
from jupyter_client.adapter import adapt
52-
from jupyter_client.jsonutil import date_default
5352
from jupyter_client.jsonutil import extract_dates
53+
from jupyter_client.jsonutil import json_default
5454
from jupyter_client.jsonutil import squash_dates
5555

5656

@@ -94,7 +94,7 @@ def squash_unicode(obj):
9494
def json_packer(obj):
9595
return jsonapi.dumps(
9696
obj,
97-
default=date_default,
97+
default=json_default,
9898
ensure_ascii=False,
9999
allow_nan=False,
100100
)

jupyter_client/tests/test_jsonutil.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ def test_parse_ms_precision():
6565
assert isinstance(parsed, str)
6666

6767

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

0 commit comments

Comments
 (0)