/// <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 override object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext) { if (pObjectToInitialize != null) { IDictionary lDictionary = pObjectToInitialize as IDictionary; if (lDictionary != null) { foreach (XElement lChild in pParentElement.Elements(XConstants.ITEM_TAG)) { IXSerializationContract lKeyContract = pSerializationContext.SelectContract(lChild.Element(XConstants.KEY_TAG), null, lDictionary.GetType().GetGenericArguments()[0], null); object lKeyObject = null; lKeyObject = lKeyContract.Read(lKeyObject, lChild.Element(XConstants.KEY_TAG), pSerializationContext); Type lValueType = lDictionary.GetType().GetGenericArguments()[1]; XElement lValueTypeElement = lChild.Element(XConstants.TYPE_TAG); if (lValueTypeElement != null) { lValueType = pSerializationContext.ResolveType(lValueTypeElement); } IXSerializationContract lValueContract = pSerializationContext.SelectContract(lChild.Element(XConstants.VALUE_TAG), null, lValueType, null); object lValueObject = null; lValueObject = lValueContract.Read(lValueObject, lChild.Element(XConstants.VALUE_TAG), pSerializationContext); lDictionary[lKeyObject] = lValueObject; } } } return(pObjectToInitialize); }
/// <summary> /// This method reads the specified p object to initialize. /// </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</returns> public override object Read(object pObjectToInitialize, XElement pElement, IXSerializationContext pSerializationContext) { PropertyInfo lPropertyInfo = pObjectToInitialize as PropertyInfo; // Look for a sub-element with the good local name. IEnumerable <XElement> lElements = pElement.Elements(lPropertyInfo.Name); XElement lPropertyElement = lElements.FirstOrDefault(); if (lPropertyElement == null) { IEnumerable <XElement> lDescendants = pElement.Descendants(lPropertyInfo.Name); lPropertyElement = lDescendants.FirstOrDefault(); } if (lPropertyElement != null) { IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lPropertyElement, lPropertyInfo.PropertyType); if (lSerializationContract != null) { object lReadObject = lSerializationContract.Read(lPropertyInfo.GetValue(pSerializationContext.CurrentObject, null), lPropertyElement, pSerializationContext); if (lReadObject == null) { lPropertyInfo.SetValue(pSerializationContext.CurrentObject, null, null); } else if (lPropertyInfo.PropertyType.IsInstanceOfType(lReadObject)) { lPropertyInfo.SetValue(pSerializationContext.CurrentObject, lReadObject, null); } return(pObjectToInitialize); } } return(null); }
/// <summary> /// Writes the specified p object. /// </summary> /// <param name="pObject">The p object.</param> /// <param name="pParentElement">The p parent element.</param> /// <param name="pSerializationContext">The p serialization context.</param> /// <returns></returns> public override XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { PropertyInfo lPropertyInfo = pObject as PropertyInfo; XElement lObjectElement = new XElement(lPropertyInfo.Name); ParameterInfo[] lParameters = lPropertyInfo.GetIndexParameters(); if (lParameters.Length == 0) { object lPropertyValue = lPropertyInfo.GetValue(pSerializationContext.CurrentObject, null); IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lObjectElement, lPropertyInfo.PropertyType); if (lPropertyValue == null) { lSerializationContract = pSerializationContext.SelectContract(lObjectElement, null); } else { lSerializationContract = pSerializationContext.SelectContract(lObjectElement, lPropertyValue); } if (lSerializationContract != null) { lSerializationContract.Write(lPropertyValue, lObjectElement, pSerializationContext); } pParentElement.Add(lObjectElement); } return(pParentElement); }
/// <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, System.Xml.Linq.XElement pElement, IXSerializationContext pSerializationContext) { IXTemplate lTemplate = pObjectToInitialize as IXTemplate; if (lTemplate != null) { Type lTemplatedObjectType = pSerializationContext.ResolveType(pElement.Element(XConstants.TYPE_TAG)); if (lTemplatedObjectType != null) { IXSerializationContract lContract = pSerializationContext.SelectContract(null, lTemplatedObjectType); object lTemplatedObject = null; if (lContract != null) { lTemplatedObject = lContract.Read(lTemplatedObject, pElement, pSerializationContext); } if (lTemplatedObject != null) { lTemplate.InitializeFrom(lTemplatedObject); } } } return(pObjectToInitialize); }
/// <summary> /// This method reads the specified object to initialize. /// </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</returns> public override object Read(object pObjectToInitialize, XElement pElement, IXSerializationContext pSerializationContext) { if (pObjectToInitialize != null) { //Get public propertyinfo and the private one bearing a XSerializationAttribute attribute. PropertyInfo[] lPublicPropertyInfos = pObjectToInitialize.GetType().GetProperties(); PropertyInfo[] lNonPublicPropertyInfos = pObjectToInitialize.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic); IEnumerable <PropertyInfo> lFilteredPropertyInfos = lPublicPropertyInfos.Union(lNonPublicPropertyInfos.Where(pElt => pElt.GetCustomAttributes(typeof(IXSerializationAttribute), true).Any())); // The order is given by the XML, not by the properties. foreach (var lElement in pElement.Elements()) { PropertyInfo lPropertyInfo = lFilteredPropertyInfos.FirstOrDefault(pProp => pProp.Name == lElement.Name); if (lPropertyInfo != null) { IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(null, lPropertyInfo); if (lSerializationContract != null) { lSerializationContract.Read(lPropertyInfo, pElement, pSerializationContext); } } else { IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lElement, null); if (lSerializationContract != null) { lSerializationContract.Read(null, lElement, pSerializationContext); } } } } return(pObjectToInitialize); }
/// <summary> /// This method serializes an object into an XElement. /// </summary> /// <param name="pObjectToSerialize">The object to serialize.</param> /// <returns>A valid XElement if the serialization succeed, null otherwise.</returns> public virtual XElement Serialize(object pObjectToSerialize) { this.IsWriting = true; this.mErrors.Clear(); XElement lParentElement = new XElement(XConstants.ROOT_TAG); IXSerializationContract lContract = this.SelectContract(null, null, null, pObjectToSerialize); if (lContract != null) { if (this.mCurrentDirectory == null) { this.mCurrentDirectory = new DirectoryInfo(Environment.CurrentDirectory); } // Write the object. lContract.Write(pObjectToSerialize, lParentElement, this); XElement lTypeContainerElement = new XElement(XConstants.TYPE_CONTAINER_TAG); foreach (KeyValuePair <Type, XElement> lTypeInfo in this.mElementsByType) { lTypeInfo.Value.SetAttributeValue(XConstants.TYPE_REF_ATTRIBUTE, this.mTypeReferencesByType[lTypeInfo.Key]); lTypeContainerElement.Add(lTypeInfo.Value); } lParentElement.AddFirst(lTypeContainerElement); return(lParentElement); } return(null); }
/// <summary> /// This method reads the specified object to initialize. /// </summary> /// <param name="pObjectToInitialize">The object to initialize.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The initialized object</returns> public override object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext) { PropertyInfo lPropertyInfo = pObjectToInitialize as PropertyInfo; // Look for a sub-element with the good local name. // ReSharper disable once PossibleNullReferenceException IEnumerable <XElement> lElements = pParentElement.Elements(lPropertyInfo.Name); XElement lPropertyElement = lElements.FirstOrDefault(); if (lPropertyElement == null) { IEnumerable <XElement> lDescendants = pParentElement.Descendants(lPropertyInfo.Name); lPropertyElement = lDescendants.FirstOrDefault(); } if (lPropertyElement != null) { IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lPropertyElement, lPropertyInfo.PropertyType); if (lSerializationContract != null) { object lReadObject = lSerializationContract.Read(lPropertyInfo.GetValue(pSerializationContext.CurrentObject, null), lPropertyElement, pSerializationContext); return(pObjectToInitialize); } } return(null); }
/// <summary> /// This method creates the specified element. /// </summary> /// <param name="pParentElement">The element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns> /// The created object. /// </returns> public object Create(XElement pParentElement, IXSerializationContext pSerializationContext) { XElement lTypeElement = pParentElement.Element(XConstants.TYPE_TAG); if (lTypeElement != null) { Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement); if (lRetrievedType != null) { XElement lKeyElement = pParentElement.Descendants(XConstants.KEY_TAG).FirstOrDefault(); IXSerializationContract lKeyContract = pSerializationContext.SelectContract(lKeyElement, null, lRetrievedType.GetGenericArguments()[0], null); object lKeyObject = null; if (lKeyContract.NeedCreate) { lKeyObject = lKeyContract.Create(lKeyElement, pSerializationContext); } else { try { lKeyObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[0], true); } catch { } } lKeyObject = lKeyContract.Read(lKeyObject, lKeyElement, pSerializationContext); XElement lValueElement = pParentElement.Descendants(XConstants.VALUE_TAG).FirstOrDefault(); IXSerializationContract lValueContract = pSerializationContext.SelectContract(lValueElement, null, lRetrievedType.GetGenericArguments()[1], null); object lValueObject = null; if (lValueContract.NeedCreate) { lValueObject = lValueContract.Create(lValueElement, pSerializationContext); } else { try { lValueObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[1], true); } catch { } } lValueObject = lValueContract.Read(lValueObject, lValueElement, pSerializationContext); return(Activator.CreateInstance(lRetrievedType, new object[] { lKeyObject, lValueObject })); } } return(null); }
/// <summary> /// This method writes the specified object. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The modified parent element</returns> public override XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { try { // Store the type. pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); // Store all items. IDictionary lDictionary = pObject as IDictionary; if (lDictionary != null) { foreach (var lKey in lDictionary.Keys) { XElement lEntryElement = new XElement(XConstants.ITEM_TAG); IXSerializationContract lKeyContract = pSerializationContext.SelectContract(lEntryElement, lKey); if (lKeyContract != null) { XElement lKeyElement = new XElement(XConstants.KEY_TAG); lKeyContract.Write(lKey, lKeyElement, pSerializationContext); lEntryElement.Add(lKeyElement); } object lValue = lDictionary[lKey]; IXSerializationContract lValueContract = pSerializationContext.SelectContract(lEntryElement, lValue); if (lValueContract != null) { // Add the type of the value. Type lValueType = lDictionary.GetType().GetGenericArguments()[1]; if (object.ReferenceEquals(lValue, null) == false) { lValueType = lValue.GetType(); } lEntryElement.Add(pSerializationContext.ReferenceType(lValueType)); XElement lValuelement = new XElement(XConstants.VALUE_TAG); lValueContract.Write(lValue, lValuelement, pSerializationContext); lEntryElement.Add(lValuelement); } pParentElement.Add(lEntryElement); } } } catch { // Hack to avoid serialization crash due to DynamicTypedObject in tactical data. Need to make a serializer but want to avoid other to stop cretaing scenario :-) } 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 deserializes a XElement into an object. /// </summary> /// <param name="pElement">The element to deserialize.</param> /// <returns>The created object, null if the deserialization failed.</returns> public virtual object Deserialize(XElement pElement) { this.IsWriting = false; this.mErrors.Clear(); XElement lTypeContainerElement = pElement.Element(XConstants.TYPE_CONTAINER_TAG); if (lTypeContainerElement != null) { this.ReadTypeContainer(lTypeContainerElement); } XElement lTypeElement = pElement.Element(XConstants.TYPE_TAG); if (lTypeElement != null) { IXSerializationContract lContract = this.SelectContract(pElement, this.ResolveType(lTypeElement)); if (lContract != null) { if (this.mCurrentDirectory == null) { this.mCurrentDirectory = new DirectoryInfo(Environment.CurrentDirectory); } object lObject = null; SerializationContractDecorator lDecoratorContract = lContract as SerializationContractDecorator; if (lDecoratorContract != null && lDecoratorContract.NeedCreate) { // The decorator read method will handle the object creation. lObject = lContract.Read(lObject, pElement, this); } else { // Forcing the object creation. lObject = lContract.Create(pElement, this); lObject = lContract.Read(lObject, pElement, this); } return(lObject); } } IXmlLineInfo lInfo = pElement; this.PushError(new XSerializationError(XErrorType.UnkwnonType, lInfo.LineNumber, lInfo.LinePosition, this.CurrentFile, string.Empty)); return(null); }
/// <summary> /// THis method serializes the object in to an XElement. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement"></param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns> /// The modified XElement. /// </returns> public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); for (int lGenericIndex = 0; lGenericIndex < pObject.GetType().GetGenericArguments().Count(); lGenericIndex++) { string lItemName = XConstants.TUPLE_ITEM_TAG + (lGenericIndex + 1); IXSerializationContract lKeyContract = pSerializationContext.SelectContract(null, null, pObject.GetPropertyType(lItemName), null); if (lKeyContract != null) { XElement lKeyElement = new XElement(lItemName); lKeyContract.Write(pObject.GetPropertyValue(lItemName), lKeyElement, pSerializationContext); pParentElement.Add(lKeyElement); } } return(pParentElement); }
/// <summary> /// This method writes the specified object. /// </summary> /// <param name="pObject">The object to serialize (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 modified parent element</returns> public virtual XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { Type lFieldType; object lFieldObject = pSerializationContext.CurrentObject.GetFieldValue(this.TypedAttribute.FieldName, out lFieldType); XElement lFieldElement = new XElement("Field"); lFieldElement.SetAttributeValue("fieldName", this.TypedAttribute.FieldName); IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(null, lFieldObject); if (lSerializationContract != null) { lSerializationContract.Write(lFieldObject, lFieldElement, pSerializationContext); } pParentElement.Add(lFieldElement); this.Attribute = null; return(pParentElement); }
/// <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 XElement. /// </returns> public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { if (pObject != null) { Type lNullableType = typeof(Nullable <>).MakeGenericType(pObject.GetType()); pParentElement.Add(pSerializationContext.ReferenceType(lNullableType)); } XElement lValueElement = new XElement(XConstants.VALUE_TAG); IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(pParentElement, pObject); if (lSerializationContract != null) { lSerializationContract.Write(pObject, lValueElement, pSerializationContext); } pParentElement.Add(lValueElement); 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 writes the specified object. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The modified parent element</returns> public override System.Xml.Linq.XElement Write(object pObject, System.Xml.Linq.XElement pParentElement, IXSerializationContext pSerializationContext) { IXTemplate lTemplate = pObject as IXTemplate; if (lTemplate != null) { // Creating the templated object. object lTemplatedObject = lTemplate.Create(); if (lTemplatedObject != null) { IXSerializationContract lContract = pSerializationContext.SelectContract(null, lTemplatedObject); if (lContract != null) { lContract.Write(lTemplatedObject, pParentElement, pSerializationContext); } } } return(pParentElement); }
/// <summary> /// This method writes the specified object. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param>lList.GetType().GetGenericArguments()[0] /// <returns>The modified parent element</returns> public override XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { // Store the type. pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); // Store all items. IList lList = pObject as IList; foreach (object lItem in lList) { XElement lItemElement = new XElement(XConstants.ITEM_TAG); IXSerializationContract lContract = pSerializationContext.SelectContract(lItemElement, lItem); if (lContract != null) { lContract.Write(lItem, lItemElement, pSerializationContext); } pParentElement.Add(lItemElement); } return(pParentElement); }
/// <summary> /// This method creates the specified element. /// </summary> /// <param name="pParentElement">The element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns> /// The created object. /// </returns> public object Create(XElement pParentElement, IXSerializationContext pSerializationContext) { XElement lTypeElement = pParentElement.Element(XConstants.TYPE_TAG); if (lTypeElement != null) { Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement); if (lRetrievedType != null) { List <object> lParameters = new List <object>(); for (int lGenericIndex = 0; lGenericIndex < lRetrievedType.GetGenericArguments().Count(); lGenericIndex++) { string lItemName = XConstants.TUPLE_ITEM_TAG + (lGenericIndex + 1); XElement lItemElement = pParentElement.Descendants(lItemName).FirstOrDefault(); IXSerializationContract lItemContract = pSerializationContext.SelectContract(lItemElement, null, lRetrievedType.GetGenericArguments()[lGenericIndex], null); object lItemObject = null; if (lItemContract.NeedCreate) { lItemObject = lItemContract.Create(lItemElement, pSerializationContext); } else { try { lItemObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[lGenericIndex], true); } catch { } } lItemObject = lItemContract.Read(lItemObject, lItemElement, pSerializationContext); lParameters.Add(lItemObject); } return(Activator.CreateInstance(lRetrievedType, lParameters.ToArray())); } } return(null); }
/// <summary> /// Writes the specified object. /// </summary> /// <param name="pObject">The object.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns></returns> public override XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); // Get public propertyinfo and the private one bearing a XSerializationAttribute attribute. PropertyInfo[] lPublicPropertyInfos = pObject.GetType().GetProperties(); PropertyInfo[] lNonPublicPropertyInfos = pObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic); PropertyInfo[] lFilteredPropertyInfos = lPublicPropertyInfos.Union(lNonPublicPropertyInfos.Where(pElt => pElt.GetCustomAttributes(typeof(IXSerializationAttribute), true).Any())).ToArray(); PropertyInfo[] lSortedFilteredPropertyInfos = lFilteredPropertyInfos.Select(pX => new { Property = pX, Attribute = (OrderXSerializationAttribute)Attribute.GetCustomAttribute(pX, typeof(OrderXSerializationAttribute), true) }).OrderBy(pX => pX.Attribute != null ? pX.Attribute.Order : Int32.MaxValue).ThenBy(pX => pX.Property.Name).Select(pX => pX.Property).ToArray(); pParentElement.SetAttributeValue(XConstants.ID_ATTRIBUTE, pSerializationContext.GetObjectReference(pObject)); foreach (PropertyInfo lPropertyInfo in lSortedFilteredPropertyInfos) { IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(null, lPropertyInfo); if (lSerializationContract != null) { lSerializationContract.Write(lPropertyInfo, pParentElement, pSerializationContext); } } return(pParentElement); }
/// <summary> /// This method reads the specified element. /// </summary> /// <param name="pObjectToInitialize">The object to initialize</param> /// <param name="pParentElement">The parent 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 pParentElement, IXSerializationContext pSerializationContext) { if (pObjectToInitialize != null) { IList lList = pObjectToInitialize as IList; if (lList != null) { foreach (XElement lChild in pParentElement.Elements(XConstants.ITEM_TAG)) { Type[] lInterfaces = lList.GetType().GetInterfaces(); Type lGenericListType = lInterfaces.FirstOrDefault(pType => pType.IsGenericType == true && pType.GetGenericTypeDefinition() == typeof(IList <>)); Type lItemType = lGenericListType.GetGenericArguments()[0]; XElement lItemTypeElement = lChild.Element(XConstants.TYPE_TAG); if (lItemTypeElement != null) { lItemType = pSerializationContext.ResolveType(lItemTypeElement); } IXSerializationContract lContract = pSerializationContext.SelectContract(lChild, lItemType); if (lContract != null) { object lItem = null; lItem = lContract.Read(lItem, lChild, pSerializationContext); lList.Add(lItem); } } } } return(pObjectToInitialize); }
/// <summary> /// THis method serializes the object in to an XElement. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement"></param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns> /// The modified XElement. /// </returns> public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); IXSerializationContract lKeyContract = pSerializationContext.SelectContract(null, null, pObject.GetPropertyType("Key"), null); if (lKeyContract != null) { XElement lKeyElement = new XElement(XConstants.KEY_TAG); lKeyContract.Write(pObject.GetPropertyValue("Key"), lKeyElement, pSerializationContext); pParentElement.Add(lKeyElement); } IXSerializationContract lValueContract = pSerializationContext.SelectContract(null, null, pObject.GetPropertyType("Value"), null); if (lValueContract != null) { XElement lValueElement = new XElement(XConstants.VALUE_TAG); lValueContract.Write(pObject.GetPropertyValue("Value"), lValueElement, pSerializationContext); pParentElement.Add(lValueElement); } return(pParentElement); }
/// <summary> /// THis method deserialized an X element in to an object. /// </summary> /// <param name="pObjectToInitialize"></param> /// <param name="pParentElement">The parent 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) { XElement lTypeElement = pParentElement.Element(XConstants.TYPE_TAG); XElement lValueElement = pParentElement.Element(XConstants.VALUE_TAG); if (lTypeElement != null && lValueElement != null) { Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement); if (lRetrievedType != null) { IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lValueElement, lRetrievedType.GetGenericArguments()[0]); if (lSerializationContract != null) { Type lValueType = lRetrievedType.GetGenericArguments()[0]; object lValue = lValueType.DefaultValue(); lValue = lSerializationContract.Read(lValue, lValueElement, pSerializationContext); return(Activator.CreateInstance(lRetrievedType, lValue)); } } } return(null); }
/// <summary> /// This method is used to select the contract according contraints. /// </summary> /// <param name="pElement">The current element.</param> /// <param name="pPropertyInfo">The current property info.</param> /// <param name="pType">The current type.</param> /// <param name="pObject">The current object.</param> /// <returns>The best contract according to constraints.</returns> public virtual IXSerializationContract SelectContract(XElement pElement, PropertyInfo pPropertyInfo, Type pType, object pObject) { XSerializationAttribute lCurrentAttribute = null; List <Tuple <IXSerializationContract, SupportPriority> > lAvailableContracts = new List <Tuple <IXSerializationContract, SupportPriority> >(); if (pElement != null) { Tuple <IXSerializationContract, SupportPriority> lElementContract = this.FindContract(pElement); if (lElementContract != null) { lAvailableContracts.Add(lElementContract); } } if (pPropertyInfo != null) { object[] lAttributes = pPropertyInfo.GetCustomAttributes(typeof(XSerializationAttribute), true); if (lAttributes.Any()) { Tuple <IXSerializationContract, SupportPriority> lAttributeContract = this.FindContract(lAttributes[0]); if (lAttributeContract != null) { lCurrentAttribute = lAttributes[0] as XSerializationAttribute; lAvailableContracts.Add(lAttributeContract); } } // Look for a contract on the type. if (lCurrentAttribute == null) { Tuple <IXSerializationContract, SupportPriority> lObjectContract = this.FindContract(pPropertyInfo); if (lObjectContract != null) { lAvailableContracts.Add(lObjectContract); } } } if (pType != null) { Tuple <IXSerializationContract, SupportPriority> lTypeContract = this.FindContract(pType); if (lTypeContract != null) { lAvailableContracts.Add(lTypeContract); } } if (pObject != null) { Tuple <IXSerializationContract, SupportPriority> lObjectContract = this.FindContract(pObject); if (lObjectContract != null) { lAvailableContracts.Add(lObjectContract); } } if (lAvailableContracts.Any()) { var lFirstOrDefault = lAvailableContracts.OrderBy(pElt => pElt.Item2).FirstOrDefault(); if (lFirstOrDefault != null) { IXSerializationContract lBestContract = lFirstOrDefault.Item1; if (lBestContract is AttributeSerializationContract && lCurrentAttribute != null) { AttributeSerializationContract lDynamicContract = new AttributeSerializationContract(); lDynamicContract.SubContract = Activator.CreateInstance(lCurrentAttribute.SupportedContract, true) as IXSerializationContract; return(lDynamicContract); } return(new SerializationContractDecorator(lBestContract, this)); } } return(this.mNullContract); }
/// <summary> /// Initializes a new instance of the <see cref="SerializationContractDecorator" /> class. /// </summary> /// <param name="pDecoratedContract">The p decorated contract.</param> /// <param name="pXSerializer">The calling x serializer.</param> public SerializationContractDecorator(IXSerializationContract pDecoratedContract, XSerializer pXSerializer) { this.DecoratedContract = pDecoratedContract; this.XSerializer = pXSerializer; }
/// <summary> /// This method is used to add a contact for this serializer only. /// </summary> /// <param name="pContract">The contract to add.</param> public virtual bool AddContract(IXSerializationContract pContract) { this.mContracts.Add(pContract); return(true); }
/// <summary> /// This method is used to add a contact for this serializer only. /// </summary> /// <param name="pContract">The contract to add.</param> public virtual bool RemoveContract(IXSerializationContract pContract) { return(this.mContracts.Remove(pContract)); }