Skip to content

Commit

Permalink
Add fallback to JSON encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Jan 25, 2022
1 parent 35cbc9c commit d2d6811
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions custom_components/grocy/json_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class GrocyJSONEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any:
"""Convert special objects."""

if isinstance(o, ProductBarcode):
return o.barcode
if isinstance(o, ProductBarcodeData):
if isinstance(o, (ProductBarcode, ProductBarcodeData)):
return o.barcode
if isinstance(o, datetime):
return o.isoformat()
Expand All @@ -26,4 +24,7 @@ def default(self, o: Any) -> Any:
if hasattr(o, "as_dict"):
return o.as_dict()

return json.JSONEncoder.default(self, o)
try:
json.JSONEncoder.default(self, o)
except TypeError:
return {"__type": str(type(o)), "repr": repr(o), "str": str(o)}

0 comments on commit d2d6811

Please sign in to comment.