Skip to content

Commit

Permalink
Use a default serializer that is not only for date types
Browse files Browse the repository at this point in the history
This would allow making more general changes here. I am also adding
serialization of bytes type into base64.
  • Loading branch information
martinRenou committed Jul 9, 2021
1 parent 42a19da commit 1363f53
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
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

0 comments on commit 1363f53

Please sign in to comment.