/// <summary> /// This method reads the specified element. /// </summary> /// <param name="pObjectToInitialize">The object to initialize</param> /// <param name="pParentElement">The element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The initialized object if the input object is valid.</returns> public virtual object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext) { object lResult = null; XAttribute lAttribute = pParentElement.Attribute(XConstants.REFERENCE_ATTRIBUTE); if (lAttribute != null) { try { int lReference = Convert.ToInt32(lAttribute.Value.Trim(), CultureInfo.InvariantCulture); lResult = pSerializationContext.GetObjectByReference(lReference); if (lResult == null) // If ref not found in cache yet, attempt to retrieve it ourselves { lResult = this.AttemptReferenceRetrieval(pObjectToInitialize, pParentElement, lReference, pSerializationContext); } } catch (FormatException) { IXmlLineInfo lInfo = pParentElement; pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } catch (OverflowException) { IXmlLineInfo lInfo = pParentElement; pSerializationContext.PushError(new XSerializationError(XErrorType.NumberOverflow, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } } return(lResult); }
/// <summary> /// This method reads the specified element. /// </summary> /// <param name="pObjectToInitialize">The object to initialize</param> /// <param name="pElement">The element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The initialized object if the input object is valid.</returns> public override object Read(object pObjectToInitialize, XElement pElement, IXSerializationContext pSerializationContext) { if (pObjectToInitialize == null) { return(null); } if (pElement == null) { return(null); } UInt32 lValue = (UInt32)(pObjectToInitialize); try { lValue = Convert.ToUInt32(pElement.Value.Trim(), CultureInfo.InvariantCulture); } catch (FormatException) { IXmlLineInfo lInfo = pElement; pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } catch (OverflowException) { IXmlLineInfo lInfo = pElement; pSerializationContext.PushError(new XSerializationError(XErrorType.NumberOverflow, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } return(lValue); }
/// <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); }
/// <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 reads the specified element. /// </summary> /// <param name="pObjectToInitialize">The object to initialize (it is the property value of the parent object)</param> /// <param name="pParentElement">The parent element (it is the property name of the parent object).</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The initialized object if the input object is valid.</returns> public virtual object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext) { PropertyInfo lPropertyInfo = pObjectToInitialize as PropertyInfo; if (lPropertyInfo != null && pSerializationContext.CurrentObject != null) { if (pParentElement.Elements(lPropertyInfo.Name).Elements("Field").Any()) { XElement lFieldElement = pParentElement.Elements(lPropertyInfo.Name).Elements("Field").FirstOrDefault(pElement => pElement.Attribute("fieldName") != null && pElement.Attribute("fieldName").Value == this.TypedAttribute.FieldName); Type lFieldType; object lFieldObject = pSerializationContext.CurrentObject.GetFieldValue(this.TypedAttribute.FieldName, out lFieldType); IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(null, lFieldType); if (lSerializationContract != null) { object lReadFieldObject = lSerializationContract.Read(lFieldObject, lFieldElement, pSerializationContext); pSerializationContext.CurrentObject.SetFieldValue(this.TypedAttribute.FieldName, lReadFieldObject); } // Check if a initial synchronisation method exits. if (string.IsNullOrWhiteSpace(this.TypedAttribute.SyncFieldMethod) == false) { MethodInfo lSynchronizationMethod = pSerializationContext.CurrentObject.GetType().GetMethod(this.TypedAttribute.SyncFieldMethod, BindingFlags.NonPublic | BindingFlags.Instance); if (lSynchronizationMethod != null) { lSynchronizationMethod.Invoke(pSerializationContext.CurrentObject, null); } } } else if (pParentElement.Elements(lPropertyInfo.Name).Any()) { object lValue = new InternalReferenceSerializationContract().Read(pObjectToInitialize, pParentElement.Elements(lPropertyInfo.Name).FirstOrDefault(), pSerializationContext); pSerializationContext.CurrentObject.SetPropertyValue(lPropertyInfo.Name, lValue); } } else { IXmlLineInfo lInfo = pParentElement; pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, "Field serialization contract must be called with a property info and not null object as parameter")); } return(pObjectToInitialize); }
/// <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; }
/// <summary> /// This method reads the specified element. /// </summary> /// <param name="pObjectToInitialize">The object to initialize</param> /// <param name="pParentElement">The element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The initialized object if the input object is valid.</returns> public virtual object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext) { XAttribute lAttribute = pParentElement.Attribute(XConstants.REFERENCE_ATTRIBUTE); try { int lReference = Convert.ToInt32(lAttribute.Value.Trim(), CultureInfo.InvariantCulture); return pSerializationContext.GetObjectByReference(lReference); } catch (FormatException) { IXmlLineInfo lInfo = pParentElement; pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } catch (OverflowException) { IXmlLineInfo lInfo = pParentElement; pSerializationContext.PushError(new XSerializationError(XErrorType.NumberOverflow, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } return null; }