Beispiel #1
0
        private object deserializeXmlElement(Type memberType, XmlElement xmlElement)
        {
            if (memberType == typeof(string))
            {
                return(xmlElement.InnerText);
            }

            if ((xmlElement.InnerText == string.Empty) && PRIMITIVE_TYPES_NULL_IS_0.Contains(memberType))
            {
                return(0);
            }

            if (memberType.IsArray)
            {
                List <XmlElement> childElements = new List <XmlElement>();
                foreach (XmlNode childNode in xmlElement.ChildNodes)
                {
                    if (childNode.NodeType == XmlNodeType.Element)
                    {
                        childElements.Add((XmlElement)childNode);
                    }
                }
                return(deserializeArray(memberType, childElements));
            }

            if (memberType.IsEnum)
            {
                return(Enum.Parse(memberType, xmlElement.InnerText));
            }

            if (Type.GetTypeCode(memberType) == TypeCode.Object)
            {
                IValueXmlSerializer serializer = GetSerializerForType(memberType);
                if (serializer == null)
                {
                    return(xmlElement.InnerText);
                }
                return(serializer.DeserializeItem(xmlElement.OfType <XmlElement>().FirstOrDefault()));
            }

            return(Convert.ChangeType(xmlElement.InnerText, memberType));
        }
Beispiel #2
0
        private object serializeValue(MemberInfo memberInfo, Type memberType, object item, int arrayDimension = 0)
        {
            if (item == null)
            {
                return(string.Empty);
            }

            IModel itemAsImodel = item as IModel;

            if (itemAsImodel != null)
            {
                return(itemAsImodel.ID);
            }

            if (memberType.IsArray && (item is Array))
            {
                Array           array         = item as Array;
                List <XElement> arrayElements = new List <XElement>();
                foreach (var element in array)
                {
                    arrayElements.Add(serializeCollectionElement(memberInfo, memberType.GetElementType(), element, arrayDimension));
                }
                return(arrayElements);
            }

            if (Type.GetTypeCode(memberType) == TypeCode.Object)
            {
                IValueXmlSerializer serializer = GetSerializerForType(memberType);
                if (serializer == null)
                {
                    return(item.ToString());
                }
                return(serializer.SerializeItem(item));
            }

            return(item.ToString());
        }
Beispiel #3
0
 public static void RegisterSerializer(IValueXmlSerializer serializer)
 {
     Serializers.Add(serializer.Type, serializer);
 }
Beispiel #4
0
        private object deserializeXmlElement(MemberInfo memberInfo, Type memberType, XmlElement xmlElement, object parentItem, int arrayDimension = 0)
        {
            if (memberType == typeof(string))
            {
                return(xmlElement.InnerText);
            }

            if ((xmlElement.InnerText == string.Empty) && PRIMITIVE_TYPES_NULL_IS_0.Contains(memberType))
            {
                return(0);
            }

            bool isCollection = memberType.GetInterfaces().Any(interfaceType => interfaceType.IsGenericType && (interfaceType.GetGenericTypeDefinition() == typeof(ICollection <>)));

            if (memberType.IsArray || isCollection)
            {
                List <XmlElement> childElements = new List <XmlElement>();
                foreach (XmlNode childNode in xmlElement.ChildNodes)
                {
                    if (childNode.NodeType == XmlNodeType.Element)
                    {
                        childElements.Add((XmlElement)childNode);
                    }
                }
                return(deserializeArray(memberInfo, memberType, childElements, parentItem, arrayDimension));
            }

            if (memberType.IsEnum)
            {
                return(Enum.Parse(memberType, xmlElement.InnerText));
            }

            Type nullableUnderlyingType = Nullable.GetUnderlyingType(memberType);

            if (nullableUnderlyingType?.IsEnum == true)
            {
                if (xmlElement.InnerText == string.Empty)
                {
                    return(null);
                }
                return(Enum.Parse(nullableUnderlyingType, xmlElement.InnerText));
            }

            Type deserializeAsType = memberType;

            if (Type.GetTypeCode(memberType) == TypeCode.Object)
            {
                PersistSubclassAttribute persistSubclassAttribute = memberInfo.GetCustomAttributes <PersistSubclassAttribute>().FirstOrDefault();
                if (persistSubclassAttribute != null) // should check if given type is subclass of member type
                {
                    deserializeAsType = persistSubclassAttribute.SubclassType;
                }

                PolymorphFieldAttribute   polymorphFieldAttribute = memberInfo.GetCustomAttributes <PolymorphFieldAttribute>().FirstOrDefault();
                Dictionary <Type, string> typeStringDictionary    = null;
                if (polymorphFieldAttribute != null)
                {
                    string     itemTypeString = xmlElement.GetAttribute(polymorphFieldAttribute.TypeAttributeName);
                    MethodInfo typeStringDictionaryGetterMethodInfo = parentItem.GetType().GetMethod(polymorphFieldAttribute.TypeStringDictionaryGetterName, memberLookupBindingFlags);
                    typeStringDictionary = typeStringDictionaryGetterMethodInfo.Invoke(parentItem, new object[] { }) as Dictionary <Type, string>;
                    KeyValuePair <Type, string> foundTypeData = typeStringDictionary.FirstOrDefault(kvp => (kvp.Value == itemTypeString));
                    if (foundTypeData.Key != null)
                    {
                        deserializeAsType = foundTypeData.Key;
                    }
                }

                IValueXmlSerializer serializer = GetSerializerForType(deserializeAsType);
                if (serializer == null)
                {
                    return(xmlElement.InnerText);
                }
                PersistAsAttribute persistData       = getPersistDataForMember(memberInfo, null, arrayDimension);
                XmlElement         itemToDeserialize = xmlElement;
                if (persistData.TagName != null)
                {
                    itemToDeserialize = itemToDeserialize.OfType <XmlElement>().FirstOrDefault();
                }
                return(serializer.DeserializeItem(itemToDeserialize, parentItem));
            }

            return(Convert.ChangeType(xmlElement.InnerText, deserializeAsType));
        }
Beispiel #5
0
        private object serializeValue(MemberInfo memberInfo, Type memberType, object item, object parentItem, int arrayDimension = 0)
        {
            if (item == null)
            {
                return(string.Empty);
            }

            ISystemObject itemAsSystemObject = item as ISystemObject;

            if ((itemAsSystemObject != null) && (memberInfo.GetCustomAttribute <PersistDetailedAttribute>() == null))
            {
                return(itemAsSystemObject.GlobalID);
            }

            if (memberType.IsArray && (item is Array))
            {
                Array           array         = item as Array;
                List <XElement> arrayElements = new List <XElement>();
                foreach (var element in array)
                {
                    arrayElements.Add(serializeCollectionElement(memberInfo, memberType.GetElementType(), element, parentItem, arrayDimension));
                }
                return(arrayElements);
            }

            bool isCollection = false;
            Type elementType  = null;

            foreach (Type interfaceType in memberType.GetInterfaces())
            {
                if (interfaceType.IsGenericType && (interfaceType.GetGenericTypeDefinition() == typeof(ICollection <>)))
                {
                    isCollection = true;
                    elementType  = interfaceType.GetGenericArguments()[0];
                    break;
                }
            }
            if (isCollection)
            {
                IEnumerable     enumerable    = item as IEnumerable;
                List <XElement> arrayElements = new List <XElement>();
                foreach (var element in enumerable)
                {
                    arrayElements.Add(serializeCollectionElement(memberInfo, elementType, element, parentItem, arrayDimension));
                }
                return(arrayElements);
            }

            if (Type.GetTypeCode(memberType) == TypeCode.Object)
            {
                Type itemType        = item.GetType();
                Type serializeAsType = memberType;

                PersistSubclassAttribute persistSubclassAttribute = memberInfo.GetCustomAttributes <PersistSubclassAttribute>().FirstOrDefault();
                if (persistSubclassAttribute != null) // should check if given type is subclass of member type
                {
                    serializeAsType = persistSubclassAttribute.SubclassType;
                }

                PolymorphFieldAttribute   polymorphFieldAttribute = memberInfo.GetCustomAttributes <PolymorphFieldAttribute>().FirstOrDefault();
                Dictionary <Type, string> typeStringDictionary    = null;
                string itemTypeString = null;
                if (polymorphFieldAttribute != null)
                {
                    MethodInfo typeStringDictionaryGetterMethodInfo = parentItem.GetType().GetMethod(polymorphFieldAttribute.TypeStringDictionaryGetterName, memberLookupBindingFlags);
                    typeStringDictionary = typeStringDictionaryGetterMethodInfo.Invoke(parentItem, new object[] { }) as Dictionary <Type, string>;
                    if (typeStringDictionary?.TryGetValue(itemType, out itemTypeString) == true)
                    {
                        serializeAsType = itemType;
                    }
                }

                IValueXmlSerializer serializer = GetSerializerForType(serializeAsType);
                if (serializer == null)
                {
                    return(item.ToString());
                }
                XElement serializedItem = serializer.SerializeItem(item, parentItem);
                if ((polymorphFieldAttribute?.TypeAttributeName != null) && (itemTypeString != null))
                {
                    serializedItem.SetAttributeValue(polymorphFieldAttribute.TypeAttributeName, itemTypeString);
                }
                return(serializedItem);
            }

            return(item.ToString());
        }