Example #1
0
        private void SerializePropertiesToNodes(XElement parent, object graph, XmlSerializeContext context)
        {
            TypeFields tf = TypeFields.GetTypeFields(graph.GetType());

            foreach (KeyValuePair <TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields)
            {
                ExtendedFieldInfo efi = kp.Value;

                if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi))
                {
                    object data = GetValueFromObject(efi.FieldInfo, graph);

                    if ((data == null || data == DBNull.Value) == false)
                    {
                        if (Type.GetTypeCode(data.GetType()) == TypeCode.Object)
                        {
                            int objID = 0;

                            if (context.ObjectContext.TryGetValue(data, out objID) == false)
                            {
                                objID = context.CurrentID;
                                context.ObjectContext.Add(data, objID);
                                context.ContainerTypeName = graph.GetType().AssemblyQualifiedName + "." + efi.FieldInfo.Name;

                                SerializeObjectToNode(parent, data, context);
                            }

                            XElement propertyElem = parent.AddChildElement("F");

                            propertyElem.SetAttributeValue("n", efi.AlternateFieldName);
                            propertyElem.SetAttributeValue("v", objID);
                            propertyElem.SetAttributeValue("r", true);
                            propertyElem.SetAttributeValue("oti", context.GetTypeID(kp.Key.ObjectType));

                            if (efi.IgnoreDeserializeError)
                            {
                                propertyElem.SetAttributeValue("ide", efi.IgnoreDeserializeError);
                            }
                        }
                        else
                        {
                            XElement propertyElem = parent.AddChildElement("F");
                            propertyElem.SetAttributeValue("n", efi.AlternateFieldName);
                            propertyElem.SetAttributeValue("v", data);
                            propertyElem.SetAttributeValue("oti", context.GetTypeID(kp.Key.ObjectType));

                            if (efi.IgnoreDeserializeError)
                            {
                                propertyElem.SetAttributeValue("ide", efi.IgnoreDeserializeError);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        private bool OnFieldCanXElementSerialize(ExtendedFieldInfo efi)
        {
            bool result = true;

            if (this.FieldCanXElementSerialize != null)
            {
                result = this.FieldCanXElementSerialize(efi);
            }

            return(result);
        }
Example #3
0
        private void DeserializeNodesToProperties(XElement parent, object graph, XmlDeserializeContext context)
        {
            TypeFields tf = TypeFields.GetTypeFields(graph.GetType());

            foreach (KeyValuePair <TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields)
            {
                ExtendedFieldInfo efi = kp.Value;

                if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi))
                {
                    System.Type realType = efi.FieldInfo.FieldType;

                    var propertiesElement = from property in parent.Descendants("F")
                                            where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName ||
                                                   property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) &&
                                            context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType
                                            select property;

                    if (propertiesElement.FirstOrDefault() == null)
                    {
                        propertiesElement = from property in parent.Descendants("Field")
                                            where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName ||
                                                   property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) &&
                                            context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType
                                            select property;
                    }

                    XElement propertyElement = propertiesElement.FirstOrDefault();

                    if (propertyElement != null)
                    {
                        object data = null;

                        if (propertyElement.AttributeWithAlterName("isRef", "r", false) == true)
                        {
                            XElement objectElement = GetObjectElementByID(context.RootElement, propertyElement.AttributeWithAlterName("value", "v", 0));

                            if (objectElement != null)
                            {
                                bool ignoreError = propertyElement.Attribute("ide", false);

                                if (ignoreError == false)
                                {
                                    XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(efi.FieldInfo);

                                    if (attr != null)
                                    {
                                        ignoreError = attr.IgnoreDeserializeError;
                                    }
                                }

                                data = DeserializeNodeToObject(objectElement, ignoreError, context);

                                SetValueToObject(efi.FieldInfo, graph, data);
                            }
                        }
                        else
                        {
                            data = propertyElement.AttributeWithAlterName("value", "v", TypeCreator.GetTypeDefaultValue(realType));

                            if (Convertible(realType, data))
                            {
                                SetValueToObject(efi.FieldInfo, graph, ConvertData(efi.FieldInfo, data));
                            }
                        }
                    }
                }
            }

            DeserializeXmlSerilizableList(parent, graph, context);
        }