private static bool ReadPropertyValue(XmlReader reader, IPropertyMap propertyMap, IXmlTemplateNode templateNode, XRoadMessage message, bool validateRequired, T dtoObject) { var templateName = propertyMap.Definition.TemplateName; var propertyNode = templateNode[templateName, message.Version]; if (propertyNode == null) { reader.ConsumeUnusedElement(); return(false); } var isNull = reader.IsNilElement(); if (validateRequired && isNull && propertyNode.IsRequired) { throw XRoadException.MissingRequiredPropertyValues(Enumerable.Repeat(propertyMap.Definition.Content.Name.LocalName, 1)); } if ((isNull || propertyMap.Deserialize(reader, dtoObject, propertyNode, message)) && !string.IsNullOrWhiteSpace(templateName)) { dtoObject.OnMemberDeserialized(templateName); } reader.ConsumeNilElement(isNull); return(propertyNode.IsRequired); }
private IXmlTemplateNode MoveToProperty(XmlReader reader, IEnumerator <IPropertyMap> properties, IXmlTemplateNode templateNode, XRoadMessage message, bool validateRequired) { IPropertyMap propertyMap = null; while (properties.MoveNext()) { propertyMap = properties.Current; var propertyName = propertyMap.Definition.GetSerializedName(); var propertyNode = templateNode[properties.Current.Definition.TemplateName, message.Version]; if (reader.LocalName == propertyName) { return(propertyNode); } if (!propertyMap.Definition.IsOptional) { throw XRoadException.UnexpectedElementInQuery(Definition.Name, propertyName, reader.LocalName); } if (validateRequired && propertyNode != null && propertyNode.IsRequired) { throw XRoadException.MissingRequiredPropertyValues(Enumerable.Repeat(propertyName, 1)); } } if (propertyMap == null) { throw XRoadException.InvalidQuery($"Element `{reader.LocalName}` was unexpected at given location while deserializing type `{Definition.Name}`."); } else { throw XRoadException.UnexpectedElementInQuery(Definition.Name, propertyMap.Definition.GetSerializedName(), reader.LocalName); } }
public override object Deserialize(XmlReader reader, IXmlTemplateNode templateNode, ContentDefinition content, XRoadMessage message) { var dtoObject = new T(); dtoObject.SetTemplateMembers(templateNode.ChildNames); var validateRequired = content.Particle is RequestDefinition; if (contentPropertyMap != null) { ReadPropertyValue(reader, contentPropertyMap, templateNode, message, validateRequired, dtoObject); return(dtoObject); } var existingPropertyNames = new HashSet <string>(); if (reader.IsEmptyElement) { ValidateRemainingProperties(existingPropertyNames, content); return(MoveNextAndReturn(reader, dtoObject)); } var parentDepth = reader.Depth; var itemDepth = parentDepth + 1; var requiredCount = 0; reader.Read(); while (parentDepth < reader.Depth) { if (reader.NodeType != XmlNodeType.Element || reader.Depth != itemDepth) { reader.Read(); continue; } var propertyMap = GetPropertyMap(reader); if (!propertyMap.Definition.Content.IsOptional) { existingPropertyNames.Add(reader.LocalName); } if (ReadPropertyValue(reader, propertyMap, templateNode, message, validateRequired, dtoObject) && validateRequired) { requiredCount++; } } if (validateRequired && requiredCount < templateNode.CountRequiredNodes(message.Version)) { throw XRoadException.MissingRequiredPropertyValues(GetMissingRequiredPropertyNames(dtoObject, templateNode, message)); } ValidateRemainingProperties(existingPropertyNames, content); return(dtoObject); }