Exemplo n.º 1
0
        Serializer CreateSerializerCore(PropertyInfo property, XmlArrayAttribute arrayAttribute, XmlArrayItemAttribute arrayItemAttribute)
        {
            if (!property.CanRead)
            {
                // TODO throw
            }
            Type ienumerable;

            if (property.PropertyType.IsGenericType &&
                property.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                ienumerable = property.PropertyType;
            }
            else
            {
                ienumerable = property.PropertyType.GetInterface("IEnumerable`1");
            }
            if (ienumerable == null)
            {
                // TODO throw
            }

            var name       = arrayAttribute.Name ?? property.Name;
            var @namespace = arrayAttribute.Namespace;
            var next       = CreateSerializer(ienumerable.GetGenericArguments()[0], arrayItemAttribute);

            if (arrayAttribute.OmitIfEmpty)
            {
                return((obj, context) => {
                    if (obj != null)
                    {
                        var first = true;
                        foreach (var item in (IEnumerable)property.GetValue(obj, null))
                        {
                            if (first)
                            {
                                context.Writer.WriteStartElement(name, @namespace);
                                first = false;
                            }
                            next(item, context);
                        }
                        if (!first)
                        {
                            context.Writer.WriteEndElement();
                        }
                    }
                });
            }
            else
            {
                return((obj, context) => {
                    context.Writer.WriteStartElement(name, @namespace);
                    if (obj != null)
                    {
                        foreach (var item in (IEnumerable)property.GetValue(obj, null))
                        {
                            next(item, context);
                        }
                    }
                    context.Writer.WriteEndElement();
                });
            }
        }
Exemplo n.º 2
0
        void ProcessMember(PropertyInfo property, List <Serializer> attributeSerializers, List <Serializer> elementSerializers)
        {
            XmlAttributeAttribute attribute_attribute  = null;
            XmlElementAttribute   element_attribute    = null;
            XmlFlagAttribute      flag_attribute       = null;
            XmlArrayAttribute     array_attribute      = null;
            XmlArrayItemAttribute array_item_attribute = null;

            foreach (var custom_attribute in property.GetCustomAttributes(false))
            {
                var attribute = custom_attribute as XmlAttributeAttribute;
                if (attribute != null)
                {
                    attribute_attribute = attribute;
                    continue;
                }

                var element = custom_attribute as XmlElementAttribute;
                if (element != null)
                {
                    element_attribute = element;
                    continue;
                }

                var flag = custom_attribute as XmlFlagAttribute;
                if (flag != null)
                {
                    flag_attribute = flag;
                    continue;
                }

                var array = custom_attribute as XmlArrayAttribute;
                if (array != null)
                {
                    array_attribute = array;
                    continue;
                }

                var array_item = custom_attribute as XmlArrayItemAttribute;
                if (array_item != null)
                {
                    array_item_attribute = array_item;
                    continue;
                }
            }

            if (attribute_attribute != null)
            {
                attributeSerializers.Add(CreateSerializer(property, attribute_attribute));
                return;
            }

            if (element_attribute != null)
            {
                elementSerializers.Add(CreateSerializer(property, element_attribute));
                return;
            }

            if (flag_attribute != null)
            {
                elementSerializers.Add(CreateSerializer(property, flag_attribute));
                return;
            }

            if (array_attribute != null)
            {
                elementSerializers.Add(CreateSerializer(property, array_attribute, array_item_attribute));
                return;
            }
        }
Exemplo n.º 3
0
 Serializer CreateSerializer(PropertyInfo property, XmlArrayAttribute arrayAttribute, XmlArrayItemAttribute arrayItemAttribute)
 {
     return(CreateSerializer(CreateSerializerCore(property, arrayAttribute, arrayItemAttribute), arrayAttribute.OmitIfNull));
 }
Exemplo n.º 4
0
        Dictionary <string, ObjectDeserializer> CreateElementAutoDeserializers(Type type, Deserializers deserializers)
        {
            var object_deserializers = new Dictionary <string, ObjectDeserializer> ();

            foreach (var property in type.GetProperties())
            {
                XmlElementAttribute   element_attribute    = null;
                XmlFlagAttribute      flag_attribute       = null;
                XmlArrayAttribute     array_attribute      = null;
                XmlArrayItemAttribute array_item_attribute = null;

                foreach (var custom_attribute in property.GetCustomAttributes(false))
                {
                    var element = custom_attribute as XmlElementAttribute;
                    if (element != null)
                    {
                        element_attribute = element;
                        continue;
                    }

                    var flag = custom_attribute as XmlFlagAttribute;
                    if (flag != null)
                    {
                        flag_attribute = flag;
                        continue;
                    }

                    var array = custom_attribute as XmlArrayAttribute;
                    if (array != null)
                    {
                        array_attribute = array;
                        continue;
                    }

                    var array_item = custom_attribute as XmlArrayItemAttribute;
                    if (array_item != null)
                    {
                        array_item_attribute = array_item;
                        continue;
                    }
                }

                if (element_attribute != null)
                {
                    var deserialization_type = GetType(element_attribute.Type, property.PropertyType);
                    var deserializer         =
                        CreateCustomDeserializer(property, deserialization_type, deserializers) ??
                        CreateElementDeserializer(property, deserialization_type);
                    AddDeserializer(object_deserializers,
                                    CreateName(property.Name, element_attribute.Name, element_attribute.Namespace),
                                    deserializer);
                    continue;
                }

                if (flag_attribute != null)
                {
                    AddDeserializer(object_deserializers,
                                    CreateName(property.Name, flag_attribute.Name, flag_attribute.Namespace),
                                    CreateElementDeserializer(property));
                    continue;
                }

                if (array_attribute != null)
                {
                    AddDeserializer(object_deserializers,
                                    CreateName(property.Name, array_attribute.Name, array_attribute.Namespace),
                                    CreateElementDeserializer(property, array_item_attribute));
                    continue;
                }
            }

            return(object_deserializers);
        }