Allow tuple subclasses to be json serialized (#74207)

pull/74522/head
J. Nick Koston 2022-06-29 19:14:56 -05:00 committed by Paulus Schoutsen
parent dbe552b1a1
commit b135560274
2 changed files with 10 additions and 1 deletions

View File

@ -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)

View File

@ -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]"