Skip to content

Commit

Permalink
Allow JSON serialization to work with json.dumps (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Jan 9, 2023
1 parent 111cfc1 commit 5b39ef3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
28 changes: 14 additions & 14 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,12 +1058,12 @@ def _validate(self, val):
@classmethod
def serialize(cls, value):
if value is None:
return 'null'
return None
return list(value) # As JSON has no tuple representation

@classmethod
def deserialize(cls, value):
if value == 'null':
if value == 'null' or value is None:
return None
return tuple(value) # As JSON has no tuple representation

Expand Down Expand Up @@ -1519,12 +1519,12 @@ def __init__(self, default=None, **params):
@classmethod
def serialize(cls, value):
if value is None:
return 'null'
return None
return value.tolist()

@classmethod
def deserialize(cls, value):
if value == 'null':
if value == 'null' or value is None:
return None
from numpy import asarray
return asarray(value)
Expand Down Expand Up @@ -1606,12 +1606,12 @@ def _validate(self, val):
@classmethod
def serialize(cls, value):
if value is None:
return 'null'
return None
return value.to_dict('records')

@classmethod
def deserialize(cls, value):
if value == 'null':
if value == 'null' or value is None:
return None
from pandas import DataFrame as pdDFrame
return pdDFrame(value)
Expand Down Expand Up @@ -1978,14 +1978,14 @@ def _validate_step(self, val, step):
@classmethod
def serialize(cls, value):
if value is None:
return 'null'
return None
if not isinstance(value, (dt.datetime, dt.date)): # i.e np.datetime64
value = value.astype(dt.datetime)
return value.strftime("%Y-%m-%dT%H:%M:%S.%f")

@classmethod
def deserialize(cls, value):
if value == 'null':
if value == 'null' or value is None:
return None
return dt.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")

Expand Down Expand Up @@ -2016,12 +2016,12 @@ def _validate_step(self, val, step):
@classmethod
def serialize(cls, value):
if value is None:
return 'null'
return None
return value.strftime("%Y-%m-%d")

@classmethod
def deserialize(cls, value):
if value == 'null':
if value == 'null' or value is None:
return None
return dt.datetime.strptime(value, "%Y-%m-%d").date()

Expand Down Expand Up @@ -2176,7 +2176,7 @@ def _validate_value(self, val, allow_None):
@classmethod
def serialize(cls, value):
if value is None:
return 'null'
return None
# List as JSON has no tuple representation
serialized = []
for v in value:
Expand All @@ -2191,7 +2191,7 @@ def serialize(cls, value):
return serialized

def deserialize(cls, value):
if value == 'null':
if value == 'null' or value is None:
return None
deserialized = []
for v in value:
Expand Down Expand Up @@ -2227,13 +2227,13 @@ def _validate_value(self, val, allow_None):
@classmethod
def serialize(cls, value):
if value is None:
return 'null'
return None
# As JSON has no tuple representation
return [v.strftime("%Y-%m-%d") for v in value]

@classmethod
def deserialize(cls, value):
if value == 'null':
if value == 'null' or value is None:
return None
# As JSON has no tuple representation
return tuple([dt.datetime.strptime(v, "%Y-%m-%d").date() for v in value])
Expand Down
20 changes: 18 additions & 2 deletions tests/API1/testdateparam.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
Unit test for Date parameters.
"""


import sys
import json
import datetime as dt
import param
from . import API1TestCase
Expand Down Expand Up @@ -52,3 +52,19 @@ def test_get_soft_bounds(self):
self.assertEqual(q.get_soft_bounds(), (dt.datetime(2017,2,1),
dt.datetime(2017,2,25)))

def test_date_serialization():
class User(param.Parameterized):
A = param.Date(default=None)

# Validate round is possible
User.param.deserialize_parameters(User.param.serialize_parameters())

if sys.version_info.major == 2:
return

serialized_data = '{"name": "User", "A": null}'
deserialized_data = {"name": "User", "A": None}

assert serialized_data == json.dumps(deserialized_data)
assert serialized_data == User.param.serialize_parameters()
assert deserialized_data == User.param.deserialize_parameters(serialized_data)

0 comments on commit 5b39ef3

Please sign in to comment.