Skip to content

Commit

Permalink
Merge pull request #175 from custom-components/Add-fallback-to-JSON-e…
Browse files Browse the repository at this point in the history
…ncoder

Add fallback to JSON encoder
  • Loading branch information
isabellaalstrom authored Jan 26, 2022
2 parents a85e86b + d2d6811 commit e2e0442
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 e2e0442

Please sign in to comment.