Skip to content

Commit

Permalink
fix options and allow default for orjson serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jkitchin committed Jun 27, 2024
1 parent 2eb1c21 commit 00e9ec4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pycse/hashcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ class SqlCache(HashCache):

cache = "cache.sqlite"

# default is a serializing function for orjson
# I guess the signature is default(self, obj)
default = None

def __init__(self, function):
self.function = function

Expand All @@ -306,7 +310,7 @@ def dump_data(self, hsh, data):
DATA must be serializable to json.
"""
value = orjson.dumps(data, option=orjson.OPT_SERIALIZE_NUMPY)
value = orjson.dumps(data, default=self.default, option=orjson.OPT_SERIALIZE_NUMPY)
with self.con:
self.con.execute("INSERT INTO cache(hash, value) VALUES(?, ?)", (hsh, value))

Expand Down Expand Up @@ -387,6 +391,8 @@ class JsonCache(HashCache):
This is compatible with maggma.
"""

default = None

def __init__(self, function):
self.function = function

Expand All @@ -401,7 +407,7 @@ def dump_data(self, hsh, data):
os.makedirs(hshpath.parent, exist_ok=True)

with open(hshpath, "wb") as f:
f.write(orjson.dumps(data))
f.write(orjson.dumps(data, default=self.default, option=orjson.OPT_SERIALIZE_NUMPY))

def load_data(self, hsh):
hshpath = self.get_hashpath(hsh).with_suffix(".json")
Expand Down Expand Up @@ -445,7 +451,7 @@ def dump(**kwargs):

os.makedirs(hshpath.parent, exist_ok=True)
with open(hshpath, "wb") as f:
f.write(orjson.dumps(data))
f.write(orjson.dumps(data, default=hc.default, option=orjson.OPT_SERIALIZE_NUMPY))
return hsh

@staticmethod
Expand Down

0 comments on commit 00e9ec4

Please sign in to comment.