From d2d6811110f6bd9bc253212f43e5353593fdc634 Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Tue, 25 Jan 2022 23:23:57 +0000 Subject: [PATCH] Add fallback to JSON encoder --- custom_components/grocy/json_encode.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/custom_components/grocy/json_encode.py b/custom_components/grocy/json_encode.py index 96ce211..eb78fbe 100644 --- a/custom_components/grocy/json_encode.py +++ b/custom_components/grocy/json_encode.py @@ -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() @@ -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)}