Skip to content

Commit

Permalink
wip: format
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Sep 24, 2024
1 parent bec1e14 commit 563ecde
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/src/prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def default(self, o):
return b64encode(o).decode()
elif isinstance(o, datetime):
return o.isoformat()
return json.JSONEncoder.encode(self, o)
return self.super().encode(o)


class JSONDecoder(json.JSONDecoder):
Expand All @@ -67,14 +67,18 @@ def object_hook(self, dct):
continue
except:
pass

if isinstance(v, dict):
dct[k] = self.object_hook(v)
return dct


class Json(Codec):
def encode(self) -> bytes:
v = self
if not isinstance(self, dict) and hasattr(self, "__dict__"):
v = self.__dict__
if not isinstance(self, (dict, datetime, bytes)) and hasattr(self, "__dict__"):
if len(self.__dict__) > 0:
v = self.__dict__
return json.dumps(v, cls=JSONEncoder).encode()

@classmethod
Expand Down Expand Up @@ -176,7 +180,7 @@ def inner(*args):
def input_json(t: Optional[type] = None):
"""Get input as JSON"""
if t is int or t is float:
return t(json.loads(input_str()))
return t(json.loads(input_str(), cls=JSONDecoder))
if issubclass(t, Json):
return t(**json.loads(input_str(), cls=JSONDecoder))
return json.loads(input_str(), cls=JSONDecoder)
Expand All @@ -187,7 +191,7 @@ def output_json(x):
if isinstance(x, int) or isinstance(x, float):
output_str(json.dumps(str(x)))
return

if hasattr(x, "__dict__"):
x = x.__dict__
output_str(json.dumps(x, cls=JSONEncoder))
Expand Down Expand Up @@ -247,7 +251,7 @@ def get_json(key: str):
x = Var.get_str(key)
if x is None:
return x
return json.loads(x)
return json.loads(x, cls=JSONDecoder)

@staticmethod
def set(key: str, value: Union[bytes, str]):
Expand All @@ -269,7 +273,7 @@ def get_json(key: str):
x = ffi.config_get(key)
if x is None:
return None
return json.loads(x)
return json.loads(x, cls=JSONDecoder)


class HttpResponse:
Expand All @@ -293,7 +297,7 @@ def data_str(self):

def data_json(self):
"""Get response body JSON"""
return json.loads(self.data_str())
return json.loads(self.data_str(), cls=JSONDecoder)


class Http:
Expand Down

0 comments on commit 563ecde

Please sign in to comment.