Esempio n. 1
0
        /// <summary>
        /// THis method deserializes an X element in to an object.
        /// </summary>
        /// <param name="pObjectToInitialize"></param>
        /// <param name="pParentElement">The element to convert.</param>
        /// <param name="pSerializationContext">The context of serialization</param>
        /// <returns>
        /// The initialized object
        /// </returns>
        public object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            try
            {
                if (this.NeedCreate)
                {
                    pObjectToInitialize = this.Create(pParentElement, pSerializationContext);
                }

                int        lReadReference = XConstants.NO_REFERENCED_OBJECT;
                XAttribute lAttribute     = pParentElement.Attribute(XConstants.ID_ATTRIBUTE);
                if (lAttribute != null)
                {
                    lReadReference = Convert.ToInt32(lAttribute.Value.Trim(), CultureInfo.InvariantCulture);
                }
                bool lCanBeCurrent = this.CanBeCurrentObject(pObjectToInitialize);
                if (lCanBeCurrent)
                {
                    pSerializationContext.PushObject(pObjectToInitialize, lReadReference);
                }
                object lInitializedObject = this.DecoratedContract.Read(pObjectToInitialize, pParentElement, pSerializationContext);
                if (lCanBeCurrent)
                {
                    pSerializationContext.PopObject();
                }
                return(lInitializedObject);
            }
            catch
            {
                IXmlLineInfo lInfo = pParentElement;
                pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty));
            }
            return(pObjectToInitialize);
        }
Esempio n. 2
0
        /// <summary>
        /// This method serializes the object in to an XElement.
        /// </summary>
        /// <param name="pObject">The object to serialize.</param>
        /// <param name="pParentElement">The parent element.</param>
        /// <param name="pSerializationContext">The context of serialization</param>
        /// <returns>
        /// The modified parent.
        /// </returns>
        public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            try
            {
                int    lReference         = XConstants.NOT_YET_REFERENCED_OBJECT;
                string lExternalReference = string.Empty;

                if (pSerializationContext.ExternalReferenceResolver.HasExternalReference(pObject) && pSerializationContext.CurrentObject != null)
                {
                    lExternalReference = pSerializationContext.ExternalReferenceResolver.GetExternalReference(pObject);
                }

                bool lCanBeCurrent    = this.CanBeCurrentObject(pObject);
                bool lCanBeReferenced = this.CanBeInternallyReferenced(pObject);
                if (lCanBeCurrent)
                {
                    if (lCanBeReferenced)
                    {
                        lReference = pSerializationContext.GetObjectReference(pObject);
                    }
                    pSerializationContext.PushObject(pObject);
                }

                XElement lModifiedElement;
                if (string.IsNullOrEmpty(lExternalReference) == false)
                {
                    pParentElement.Add(new XComment("Serialized with XSerialization.Values.ExternalReferenceSerializationContract"));
                    lModifiedElement = new ExternalReferenceSerializationContract().Write(pObject, pParentElement, pSerializationContext);
                }
                else if (lReference != XConstants.NOT_YET_REFERENCED_OBJECT)
                {
                    pParentElement.Add(new XComment("Serialized with XSerialization.Values.InternalReferenceSerializationContract"));
                    lModifiedElement = new InternalReferenceSerializationContract().Write(pObject, pParentElement, pSerializationContext);
                }
                else
                {
                    pParentElement.Add(new XComment("Serialized with " + this.DecoratedContract.GetType().XmlFullname()));
                    lModifiedElement = this.DecoratedContract.Write(pObject, pParentElement, pSerializationContext);
                }

                if (lCanBeCurrent)
                {
                    pSerializationContext.PopObject();
                }
                return(lModifiedElement);
            }
            catch
            {
                IXmlLineInfo lInfo = pParentElement;
                pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty));
            }
            return(pParentElement);
        }
        /// <summary>
        /// Attempt to retrieve the missing reference (last chance)
        /// </summary>
        /// <param name="pObjectToInitialize">The object to initialize</param>
        /// <param name="pParentElement"></param>
        /// <param name="pReference">The reference to look for.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The extracted object, null otherwise.</returns>
        private object AttemptReferenceRetrieval(object pObjectToInitialize, XElement pParentElement, int pReference, IXSerializationContext pSerializationContext)
        {
            object   lResult = null;
            XElement lXRoot  = this.GetRoot(pParentElement);

            if (lXRoot != null)
            {
                IEnumerable <XElement> lXElements = lXRoot.Descendants(pParentElement.Name);
                if (lXElements != null &&
                    lXElements.Any())
                {
                    foreach (XElement lXElement in lXElements)
                    {
                        XAttribute lXId = lXElement.Attribute(XConstants.ID_ATTRIBUTE);
                        if (lXId != null)
                        {
                            int lId = Convert.ToInt32(lXId.Value.Trim(), CultureInfo.InvariantCulture);
                            if (lId == pReference)
                            {
                                XElement lXType = lXElement.Element(XConstants.TYPE_TAG);
                                if (lXType != null)
                                {
                                    Type lType = pSerializationContext.ResolveType(lXType);
                                    IXSerializationContract lContract = pSerializationContext.SelectContract(lXElement, null, lType, pObjectToInitialize);
                                    if (lContract != null)
                                    {
                                        if (lContract.NeedCreate)
                                        {
                                            pObjectToInitialize = lContract.Create(lXElement, pSerializationContext);
                                        }

                                        lResult = lContract.Read(pObjectToInitialize, lXElement, pSerializationContext);
                                        if (lResult != null)
                                        {
                                            pSerializationContext.PushObject(lResult, pReference); // Cache it into ref by obj caches.
                                            pSerializationContext.PopObject();                     // But remove it from current object stack then to come back to previous current objects stack state.
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }

            return(lResult);
        }
        /// <summary>
        /// This method serializes the object in to an XElement.
        /// </summary>
        /// <param name="pObject">The object to serialize.</param>
        /// <param name="pParentElement">The parent element.</param>
        /// <param name="pSerializationContext">The context of serialization</param>
        /// <returns>
        /// The modified parent.
        /// </returns>
        public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            try
            {
                int lReference = XConstants.NOT_YET_REFERENCED_OBJECT;
                string lExternalReference = string.Empty;

                if (pSerializationContext.ExternalReferenceResolver.HasExternalReference(pObject) && pSerializationContext.CurrentObject != null)
                {
                    lExternalReference = pSerializationContext.ExternalReferenceResolver.GetExternalReference(pObject);
                }

                bool lCanBeCurrent = this.CanBeCurrentObject(pObject);
                bool lCanBeReferenced = this.CanBeInternallyReferenced(pObject);
                if (lCanBeCurrent)
                {
                    if (lCanBeReferenced)
                    {
                        lReference = pSerializationContext.GetObjectReference(pObject);
                    }
                    pSerializationContext.PushObject(pObject);
                }

                XElement lModifiedElement;
                if (string.IsNullOrEmpty(lExternalReference) == false)
                {
                    pParentElement.Add(new XComment("Serialized with XSerialization.Values.ExternalReferenceSerializationContract"));
                    lModifiedElement = new ExternalReferenceSerializationContract().Write(pObject, pParentElement, pSerializationContext);
                }
                else if (lReference != XConstants.NOT_YET_REFERENCED_OBJECT)
                {
                    pParentElement.Add(new XComment("Serialized with XSerialization.Values.InternalReferenceSerializationContract"));
                    lModifiedElement = new InternalReferenceSerializationContract().Write(pObject, pParentElement, pSerializationContext);
                }
                else
                {
                    pParentElement.Add(new XComment("Serialized with " + this.DecoratedContract.GetType().XmlFullname()));
                    lModifiedElement = this.DecoratedContract.Write(pObject, pParentElement, pSerializationContext);
                }

                if (lCanBeCurrent)
                {
                    pSerializationContext.PopObject();
                }
                return lModifiedElement;
            }
            catch
            {
                IXmlLineInfo lInfo = pParentElement;
                pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty));
            }
            return pParentElement;
        }
        /// <summary>
        /// THis method deserializes an X element in to an object.
        /// </summary>
        /// <param name="pObjectToInitialize"></param>
        /// <param name="pParentElement">The element to convert.</param>
        /// <param name="pSerializationContext">The context of serialization</param>
        /// <returns>
        /// The initialized object
        /// </returns>
        public object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            try
            {
                if (this.NeedCreate)
                {
                    pObjectToInitialize = this.Create(pParentElement, pSerializationContext);
                }

                int lReadReference = XConstants.NO_REFERENCED_OBJECT;
                XAttribute lAttribute = pParentElement.Attribute(XConstants.ID_ATTRIBUTE);
                if (lAttribute != null)
                {
                    lReadReference = Convert.ToInt32(lAttribute.Value.Trim(), CultureInfo.InvariantCulture);
                }
                bool lCanBeCurrent = this.CanBeCurrentObject(pObjectToInitialize);
                if (lCanBeCurrent)
                {
                    pSerializationContext.PushObject(pObjectToInitialize, lReadReference);
                }
                object lInitializedObject = this.DecoratedContract.Read(pObjectToInitialize, pParentElement, pSerializationContext);
                if (lCanBeCurrent)
                {
                    pSerializationContext.PopObject();
                }
                return lInitializedObject;
            }
            catch
            {
                IXmlLineInfo lInfo = pParentElement;
                pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty));
            }
            return pObjectToInitialize;
        }