Skip to content

Commit 59fbd1d

Browse files
committed
deepdiff seralizing python dict keys
1 parent 43acb92 commit 59fbd1d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

deepdiff/serialization.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import ipaddress
1515
from copy import deepcopy, copy
1616
from functools import partial
17-
from collections.abc import Mapping
17+
from collections.abc import Mapping, KeysView
1818
from typing import (
1919
Callable, Optional, Union,
2020
overload, Literal, Any,
@@ -93,6 +93,7 @@ class UnsupportedFormatErr(TypeError):
9393
'ipaddress.IPv6Network',
9494
'ipaddress.IPv4Address',
9595
'ipaddress.IPv6Address',
96+
'collections.abc.KeysView',
9697
}
9798

9899

@@ -124,6 +125,7 @@ class UnsupportedFormatErr(TypeError):
124125
'iprange': str,
125126
'IPv4Address': ipaddress.IPv4Address,
126127
'IPv6Address': ipaddress.IPv6Address,
128+
'KeysView': list,
127129
}
128130

129131

@@ -624,6 +626,7 @@ def _serialize_tuple(value):
624626
NotPresent: str,
625627
ipranges: str,
626628
memoryview: lambda x: x.tobytes(),
629+
KeysView: list,
627630
}
628631

629632
if PydanticBaseModel is not pydantic_base_model_type:

tests/test_serialization.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,8 @@ def test_reversed_list(self):
450450

451451
assert '[3,2,1]' == serialized
452452
assert '[3,2,1]' == serialized2, "We should have copied the original list. If this returns empty, it means we exhausted the original list."
453+
454+
def test_dict_keys(self):
455+
dic = {"foo": "bar", "apple": "too sweet"}
456+
serialized = json_dumps(dic.keys())
457+
assert '["foo","apple"]' == serialized

0 commit comments

Comments
 (0)