mirror of
https://github.com/jellyfin/jellyfin-kodi.git
synced 2025-06-01 13:56:13 +00:00
Clean json returned from server for keys with None values
Add testing
This commit is contained in:
parent
8a3ca73d52
commit
ed96dc8ad5
7 changed files with 188 additions and 4 deletions
43
jellyfin_kodi/jellyfin/utils.py
Normal file
43
jellyfin_kodi/jellyfin/utils.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
from six import string_types
|
||||
from six.moves import collections_abc
|
||||
|
||||
|
||||
def clean_none_dict_values(obj):
|
||||
"""
|
||||
Recursively remove keys with a value of None
|
||||
"""
|
||||
if not isinstance(obj, collections_abc.Iterable) or isinstance(obj, string_types):
|
||||
return obj
|
||||
|
||||
queue = [obj]
|
||||
|
||||
while queue:
|
||||
item = queue.pop()
|
||||
|
||||
if isinstance(item, collections_abc.Mapping):
|
||||
mutable = isinstance(item, collections_abc.MutableMapping)
|
||||
remove = []
|
||||
|
||||
for key, value in item.items():
|
||||
if value is None and mutable:
|
||||
remove.append(key)
|
||||
|
||||
elif isinstance(value, string_types):
|
||||
continue
|
||||
|
||||
elif isinstance(value, collections_abc.Iterable):
|
||||
queue.append(value)
|
||||
|
||||
if mutable:
|
||||
# Remove keys with None value
|
||||
for key in remove:
|
||||
item.pop(key)
|
||||
|
||||
elif isinstance(item, collections_abc.Iterable):
|
||||
for value in item:
|
||||
if value is None or isinstance(value, string_types):
|
||||
continue
|
||||
elif isinstance(value, collections_abc.Iterable):
|
||||
queue.append(value)
|
||||
|
||||
return obj
|
Loading…
Add table
Add a link
Reference in a new issue