From e2fe1a1c5dcfc385b000d5a7edbd4a03e6ec18cd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" <nick@koston.org> Date: Wed, 29 Jun 2022 19:14:56 -0500 Subject: [PATCH] Allow tuple subclasses to be json serialized (#74207) --- homeassistant/helpers/json.py | 2 +- tests/helpers/test_json.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/json.py b/homeassistant/helpers/json.py index 8b91f5eb2b5..74a2f542910 100644 --- a/homeassistant/helpers/json.py +++ b/homeassistant/helpers/json.py @@ -33,7 +33,7 @@ def json_encoder_default(obj: Any) -> Any: Hand other objects to the original method. """ - if isinstance(obj, set): + if isinstance(obj, (set, tuple)): return list(obj) if isinstance(obj, float): return float(obj) diff --git a/tests/helpers/test_json.py b/tests/helpers/test_json.py index cfb403ca4a9..54c488690fa 100644 --- a/tests/helpers/test_json.py +++ b/tests/helpers/test_json.py @@ -1,6 +1,7 @@ """Test Home Assistant remote methods and classes.""" import datetime import json +import time import pytest @@ -87,3 +88,11 @@ def test_json_dumps_float_subclass(): """A float subclass.""" assert json_dumps({"c": FloatSubclass(1.2)}) == '{"c":1.2}' + + +def test_json_dumps_tuple_subclass(): + """Test the json dumps a tuple subclass.""" + + tt = time.struct_time((1999, 3, 17, 32, 44, 55, 2, 76, 0)) + + assert json_dumps(tt) == "[1999,3,17,32,44,55,2,76,0]" -- GitLab