Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Check for null values
Browse files Browse the repository at this point in the history
  • Loading branch information
samwelkanda committed Feb 29, 2024
1 parent 91014bf commit d1b691b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
1 change: 0 additions & 1 deletion kiota_serialization_json/json_parse_node_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from io import BytesIO

from kiota_abstractions.serialization import ParseNode, ParseNodeFactory

Expand Down
47 changes: 24 additions & 23 deletions kiota_serialization_json/json_serialization_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,32 +425,33 @@ def write_any_value(self, key: Optional[str], value: Any) -> Any:
key (Optional[str]): The key to be used for the written value. May be null.
value Any): The value to be written.
"""
if value:
value_type = type(value)
if value_type in PRIMITIVE_TYPES:
method = getattr(self, f'write_{value_type.__name__.lower()}_value')
method(key, value)
elif isinstance(value, Parsable):
self.write_object_value(key, value)
elif isinstance(value, list):
if all(isinstance(x, Parsable) for x in value):
self.write_collection_of_object_values(key, value)
elif all(isinstance(x, Enum) for x in value):
self.write_collection_of_enum_values(key, value)
elif all((type(x) in PRIMITIVE_TYPES) for x in value):
self.write_collection_of_primitive_values(key, value)
else:
raise TypeError(
f"Encountered an unknown collection type during serialization \
{value_type} with key {key}"
)
elif hasattr(value, '__dict__'):
self.write_non_parsable_object_value(key, value)
value_type = type(value)
if value is None:
self.write_null_value(key)
elif value_type in PRIMITIVE_TYPES:
method = getattr(self, f'write_{value_type.__name__.lower()}_value')
method(key, value)
elif isinstance(value, Parsable):
self.write_object_value(key, value)
elif isinstance(value, list):
if all(isinstance(x, Parsable) for x in value):
self.write_collection_of_object_values(key, value)
elif all(isinstance(x, Enum) for x in value):
self.write_collection_of_enum_values(key, value)
elif all((type(x) in PRIMITIVE_TYPES) for x in value):
self.write_collection_of_primitive_values(key, value)
else:
raise TypeError(
f"Encountered an unknown type during serialization {value_type} \
with key {key}"
f"Encountered an unknown collection type during serialization \
{value_type} with key {key}"
)
elif hasattr(value, '__dict__'):
self.write_non_parsable_object_value(key, value)
else:
raise TypeError(
f"Encountered an unknown type during serialization {value_type} \
with key {key}"
)

def _serialize_value(self, temp_writer: JsonSerializationWriter, value: U):
if on_before := self.on_before_object_serialization:
Expand Down

0 comments on commit d1b691b

Please sign in to comment.