in azext_iot/sdk/deviceupdate/controlplane/_serialization.py [0:0]
def _deserialize(self, target_obj, data):
"""Call the deserializer on a model.
Data needs to be already deserialized as JSON or XML ElementTree
:param str target_obj: Target data type to deserialize to.
:param object data: Object to deserialize.
:raises: DeserializationError if deserialization fails.
:return: Deserialized object.
"""
# This is already a model, go recursive just in case
if hasattr(data, "_attribute_map"):
constants = [name for name, config in getattr(data, '_validation', {}).items()
if config.get('constant')]
try:
for attr, mapconfig in data._attribute_map.items():
if attr in constants:
continue
value = getattr(data, attr)
if value is None:
continue
local_type = mapconfig['type']
internal_data_type = local_type.strip('[]{}')
if internal_data_type not in self.dependencies or isinstance(internal_data_type, Enum):
continue
setattr(
data,
attr,
self._deserialize(local_type, value)
)
return data
except AttributeError:
return
response, class_name = self._classify_target(target_obj, data)
if isinstance(response, basestring):
return self.deserialize_data(data, response)
elif isinstance(response, type) and issubclass(response, Enum):
return self.deserialize_enum(data, response)
if data is None:
return data
try:
attributes = response._attribute_map
d_attrs = {}
for attr, attr_desc in attributes.items():
# Check empty string. If it's not empty, someone has a real "additionalProperties"...
if attr == "additional_properties" and attr_desc["key"] == '':
continue
raw_value = None
# Enhance attr_desc with some dynamic data
attr_desc = attr_desc.copy() # Do a copy, do not change the real one
internal_data_type = attr_desc["type"].strip('[]{}')
if internal_data_type in self.dependencies:
attr_desc["internalType"] = self.dependencies[internal_data_type]
for key_extractor in self.key_extractors:
found_value = key_extractor(attr, attr_desc, data)
if found_value is not None:
if raw_value is not None and raw_value != found_value:
msg = ("Ignoring extracted value '%s' from %s for key '%s'"
" (duplicate extraction, follow extractors order)" )
_LOGGER.warning(
msg,
found_value,
key_extractor,
attr
)
continue
raw_value = found_value
value = self.deserialize_data(raw_value, attr_desc['type'])
d_attrs[attr] = value
except (AttributeError, TypeError, KeyError) as err:
msg = "Unable to deserialize to object: " + class_name
raise_with_traceback(DeserializationError, msg, err)
else:
additional_properties = self._build_additional_properties(attributes, data)
return self._instantiate_model(response, d_attrs, additional_properties)