Skip to content

Commit

Permalink
Merge pull request #386 from Acurisu/json_serialize_none_in_list
Browse files Browse the repository at this point in the history
correctly serialize None in a list
  • Loading branch information
andrueastman authored Oct 16, 2024
2 parents a3493a5 + 7c5118a commit 0a02628
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
U = TypeVar("U", bound=Parsable)
K = TypeVar("K", bound=Enum)
PRIMITIVE_TYPES = [bool, str, int, float, UUID, datetime, timedelta, date, time, bytes, Enum]
PRIMITIVE_TYPES_WITH_NONE = PRIMITIVE_TYPES + [type(None)]


class JsonSerializationWriter(SerializationWriter):
Expand Down Expand Up @@ -341,7 +342,7 @@ def write_null_value(self, key: Optional[str]) -> None:
if key:
self.writer[key] = None
else:
self.value = "null"
self.value = None

def __write_dict_value(self, key: Optional[str], value: Dict[str, Any]) -> None:
"""Writes the specified dictionary value to the stream with an optional given key.
Expand Down Expand Up @@ -474,7 +475,7 @@ def write_any_value(self, key: Optional[str], value: Any) -> Any:
elif all(isinstance(x, Enum) for x in value):
self.write_collection_of_enum_values(key, value)
elif all(
any(isinstance(x, primitive_type) for primitive_type in PRIMITIVE_TYPES)
any(isinstance(x, primitive_type) for primitive_type in PRIMITIVE_TYPES_WITH_NONE)
for x in value
):
self.write_collection_of_primitive_values(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ def test_write_additional_data_value(user_1, user_2):
"groups": [{
"friends": [user_2]
}]
}
},
"pinnedItems": [None, None]
}
)
content = json_serialization_writer.get_serialized_content()
Expand All @@ -272,4 +273,5 @@ def test_write_additional_data_value(user_1, user_2):
'"approvers": [{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\
'"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '\
'{"display_name": "John Doe", "age": 32}], "created_at": "2022-01-27", '\
'"data": {"groups": [{"friends": [{"display_name": "John Doe", "age": 32}]}]}}'
'"data": {"groups": [{"friends": [{"display_name": "John Doe", "age": 32}]}]}, '\
'"pinnedItems": [null, null]}'

0 comments on commit 0a02628

Please sign in to comment.