public static object Deserialize(PropertyInfo prop, XElement parentElement, object item, SerializerOptions options)
        {
            var tags = prop.GetAttributes <DFeItemAttribute>();

            foreach (var att in tags)
            {
                var node = parentElement.ElementsAnyNs(att.Name).FirstOrDefault();
                if (node == null)
                {
                    continue;
                }

                var objectType = ObjectType.From(att.Tipo);
                if (objectType.IsIn(ObjectType.ArrayType, ObjectType.EnumerableType))
                {
                    var listElement = parentElement.ElementsAnyNs(att.Name);
                    var list        = (ArrayList)CollectionSerializer.Deserialize(typeof(ArrayList), listElement, options);
                    var type        = CollectionSerializer.GetItemType(att.Tipo);
                    return(objectType == ObjectType.ArrayType ? list.ToArray(type) : list.Cast(type));
                }

                if (objectType == ObjectType.ListType)
                {
                    var listElement = parentElement.ElementsAnyNs(att.Name);
                    return(CollectionSerializer.Deserialize(att.Tipo, listElement, options));
                }

                return(objectType == ObjectType.ValueElementType ? ValueElementSerializer.Deserialize(att.Tipo, parentElement, options) :
                       ObjectSerializer.Deserialize(att.Tipo, node, options));
            }

            return(null);
        }
Beispiel #2
0
        public static XElement[] SerializeElementValue(ICollection values, DFeCollectionAttribute tag, DFeItemAttribute[] itemTags, SerializerOptions options)
        {
            var retElements = new List <XElement>();

            foreach (var value in values)
            {
                var itemTag = itemTags.SingleOrDefault(x => x.Tipo == value.GetType());
                Guard.Against <ACBrDFeException>(itemTag == null, $"Item {value.GetType().Name} não presente na lista de itens.");

                var properties = value.GetType().GetProperties()
                                 .Where(x => !x.ShouldIgnoreProperty() && x.ShouldSerializeProperty(value))
                                 .OrderBy(x => x.GetAttribute <DFeBaseAttribute>()?.Ordem ?? 0).ToArray();

                var valueProp = properties.Single(x => x.HasAttribute <DFeItemValueAttribute>());

                var valueType = ObjectType.From(valueProp.PropertyType);
                Guard.Against <ACBrDFeException>(valueType != ObjectType.PrimitiveType,
                                                 $"Item {value.GetType().Name} é do tipo [ItemValue] e o [DFeItemValueAttribute] não é do tipo primitivo");

                var attProps = properties.Where(x => x.HasAttribute <DFeAttributeAttribute>()).ToArray();

                var element = ValueElementSerializer.Serialize(itemTag.Name, itemTag.Namespace, value, options, valueProp, attProps);
                retElements.Add(element);
            }

            return(retElements.ToArray());
        }
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            var value         = prop.GetValue(parentObject, null);
            var objectType    = ObjectType.From(value);
            var attributes    = prop.GetAttributes <DFeItemAttribute>();
            var itemAttribute = attributes.SingleOrDefault(x => x.Tipo == value.GetType());

            Guard.Against <ACBrDFeException>(itemAttribute == null, $"Nenhum atributo [{nameof(DFeItemAttribute)}] encontrado " +
                                             $"para o objeto: {nameof(value.GetType)}");

            if (objectType.IsIn(ObjectType.ListType, ObjectType.ArrayType, ObjectType.EnumerableType))
            {
                var list = (ICollection)prop.GetValue(parentObject, null);
                return(CollectionSerializer.SerializeObjects(list, itemAttribute, options));
            }

            return(objectType == ObjectType.ValueElementType ? ValueElementSerializer.Serialize(prop, parentObject, options) :
                   new XObject[] { ObjectSerializer.Serialize(value, value.GetType(), itemAttribute.Name, itemAttribute.Namespace, options) });
        }
Beispiel #4
0
        /// <summary>
        /// Deserializes the specified type.
        /// </summary>
        /// <param name="type">The type of the list to deserialize.</param>
        /// <param name="parent">The parent.</param>
        /// <param name="prop">The property.</param>
        /// <param name="parentItem"></param>
        /// <param name="options">Indicates how the output is deserialized.</param>
        /// <returns>The deserialized list from the XElement.</returns>
        /// <exception cref="System.InvalidOperationException">Could not deserialize this non generic dictionary without more type information.</exception>
        /// Deserializes the XElement to the list (e.g. List<T />, Array of a specified type using options.
        public static object Deserialize(Type type, XElement[] parent, PropertyInfo prop, object parentItem, SerializerOptions options)
        {
            var listItemType = GetListType(type);
            var objectType   = ObjectType.From(GetItemType(prop.PropertyType));

            var list       = (IList)Activator.CreateInstance(type);
            var elementAtt = prop.GetAttribute <DFeCollectionAttribute>();

            if (prop.HasAttribute <DFeItemAttribute>())
            {
                var itemTags = prop.GetAttributes <DFeItemAttribute>();
                var elements = parent.All(x => x.Name.LocalName == elementAtt.Name) && parent.Length > 1 ? parent : parent.Elements();

                foreach (var element in elements)
                {
                    var itemTag = itemTags.SingleOrDefault(x => x.Name == element.Name.LocalName);
                    Guard.Against <ACBrDFeException>(itemTag == null, $"Nenhum atributo [{nameof(DFeItemAttribute)}] encontrado " +
                                                     $"para o elemento: {element.Name.LocalName}");

                    object item;
                    if (objectType == ObjectType.ValueElementType)
                    {
                        item = ValueElementSerializer.Deserialize(itemTag.Tipo, element, options);
                    }
                    else if (objectType == ObjectType.PrimitiveType)
                    {
                        item = PrimitiveSerializer.Deserialize(elementAtt, element, parentItem, prop);
                    }
                    else
                    {
                        item = ObjectSerializer.Deserialize(itemTag.Tipo, element, options);
                    }

                    list.Add(item);
                }
            }
            else
            {
                if (objectType == ObjectType.PrimitiveType)
                {
                    foreach (var element in parent)
                    {
                        var obj = PrimitiveSerializer.Deserialize(elementAtt, element, parentItem, prop);
                        list.Add(obj);
                    }
                }
                else
                {
                    if (ObjectType.From(prop.PropertyType).IsIn(ObjectType.ArrayType, ObjectType.EnumerableType))
                    {
                        listItemType = GetItemType(prop.PropertyType);
                    }

                    foreach (var element in parent)
                    {
                        var item = objectType == ObjectType.ValueElementType ? ValueElementSerializer.Deserialize(listItemType, element, options) :
                                   ObjectSerializer.Deserialize(listItemType, element, options);
                        list.Add(item);
                    }
                }
            }

            return(list);
        }
        public static IEnumerable <XObject> Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            try
            {
                var objectType = ObjectType.From(prop.PropertyType);

                if (objectType == ObjectType.DictionaryType)
                {
                    return(DictionarySerializer.Serialize(prop, parentObject, options));
                }

                if (objectType.IsIn(ObjectType.ListType, ObjectType.ArrayType, ObjectType.EnumerableType))
                {
                    return(CollectionSerializer.Serialize(prop, parentObject, options));
                }

                var value = prop.GetValue(parentObject, null);

                if (objectType.IsIn(ObjectType.InterfaceType, ObjectType.AbstractType))
                {
                    return(value == null ? null : InterfaceSerializer.Serialize(prop, parentObject, options));
                }

                if (objectType == ObjectType.ValueElementType)
                {
                    return(value == null ? null : ValueElementSerializer.Serialize(prop, parentObject, options));
                }

                if (objectType == ObjectType.ClassType)
                {
                    var attribute = prop.GetAttribute <DFeElementAttribute>();
                    if (attribute.Ocorrencia == Ocorrencia.NaoObrigatoria && value == null)
                    {
                        return(null);
                    }

                    return(new XObject[] { Serialize(value, prop.PropertyType, attribute.Name, attribute.Namespace, options) });
                }

                if (objectType == ObjectType.RootType)
                {
                    if (prop.HasAttribute <DFeElementAttribute>())
                    {
                        var attribute = prop.GetAttribute <DFeElementAttribute>();
                        if (attribute.Ocorrencia == Ocorrencia.NaoObrigatoria && value == null)
                        {
                            return(null);
                        }
                        return(new XObject[] { Serialize(value, prop.PropertyType, attribute.Name, attribute.Namespace, options) });
                    }

                    if (value == null)
                    {
                        return(null);
                    }
                    var rooTag   = prop.PropertyType.GetAttribute <DFeRootAttribute>();
                    var rootName = rooTag.Name;

                    if (rootName.IsEmpty())
                    {
                        var root = prop.PropertyType.GetRootName(value);
                        rootName = root.IsEmpty() ? prop.PropertyType.Name : root;
                    }

                    var rootElement = Serialize(value, prop.PropertyType, rootName, rooTag.Namespace, options);
                    return(new XObject[] { rootElement });
                }

                var tag = prop.GetElementAtt();
                return(new[] { PrimitiveSerializer.Serialize(tag, parentObject, prop, options) });
            }
            catch (Exception e)
            {
                var msg = $"Erro ao serializar a propriedade:{Environment.NewLine}{prop.DeclaringType?.Name ?? prop.PropertyType.Name} - {prop.Name}";
                logger.Error(msg, e);
                throw new ACBrDFeException(msg, e);
            }
        }
        public static object Deserialize(PropertyInfo prop, XElement parentElement, object item, SerializerOptions options)
        {
            try
            {
                var tag = prop.GetElementAtt();

                var objectType = ObjectType.From(prop.PropertyType);
                if (objectType == ObjectType.DictionaryType)
                {
                    var dicTag            = prop.GetAttribute <DFeDictionaryAttribute>();
                    var dictionaryElement = parentElement.ElementAnyNs(dicTag.Name);
                    return(DictionarySerializer.Deserialize(prop, dictionaryElement, item, options));
                }

                if (objectType.IsIn(ObjectType.ArrayType, ObjectType.EnumerableType))
                {
                    var listElement = parentElement.GetElements(prop);

                    var list = (ArrayList)CollectionSerializer.Deserialize(typeof(ArrayList), listElement.ToArray(), prop, item, options);
                    var type = prop.PropertyType.IsArray ? prop.PropertyType.GetElementType() : prop.PropertyType.GetGenericArguments()[0];
                    return(objectType == ObjectType.ArrayType ? list.ToArray(type) : list.Cast(type));
                }

                if (objectType == ObjectType.ListType)
                {
                    var listElement = parentElement.GetElements(prop);
                    return(CollectionSerializer.Deserialize(prop.PropertyType, listElement.ToArray(), prop, item, options));
                }

                if (objectType.IsIn(ObjectType.InterfaceType, ObjectType.AbstractType))
                {
                    return(InterfaceSerializer.Deserialize(prop, parentElement, item, options));
                }

                if (objectType == ObjectType.ValueElementType)
                {
                    var xElement = parentElement.ElementsAnyNs(tag.Name).FirstOrDefault();
                    return(ValueElementSerializer.Deserialize(prop.PropertyType, xElement, options));
                }

                if (objectType == ObjectType.RootType)
                {
                    if (tag != null)
                    {
                        var xElement = parentElement.ElementsAnyNs(tag.Name).FirstOrDefault();
                        return(Deserialize(prop.PropertyType, xElement, options));
                    }

                    var rootTag   = prop.PropertyType.GetAttribute <DFeRootAttribute>();
                    var rootNames = new List <string>();
                    if (!rootTag.Name.IsEmpty())
                    {
                        rootNames.Add(rootTag.Name);
                        rootNames.Add(prop.PropertyType.Name);
                    }
                    else
                    {
                        rootNames.AddRange(prop.PropertyType.GetRootNames());
                        rootNames.Add(prop.PropertyType.Name);
                    }

                    var xmlNode = (from node in parentElement.Elements()
                                   where node.Name.LocalName.IsIn(rootNames)
                                   select node).FirstOrDefault();

                    return(Deserialize(prop.PropertyType, xmlNode, options));
                }

                if (objectType == ObjectType.ClassType)
                {
                    var xElement = parentElement.ElementsAnyNs(tag.Name).FirstOrDefault();
                    return(Deserialize(prop.PropertyType, xElement, options));
                }

                XObject element;
                if (tag is DFeAttributeAttribute)
                {
                    element = parentElement.Attributes(tag.Name).FirstOrDefault();
                }
                else
                {
                    element = parentElement.ElementsAnyNs(tag.Name).FirstOrDefault();
                }

                return(PrimitiveSerializer.Deserialize(tag, element, item, prop));
            }
            catch (Exception e)
            {
                var msg = $"Erro ao deserializar a propriedade:{Environment.NewLine}{prop.DeclaringType?.Name ?? prop.PropertyType.Name} - {prop.Name}";
                logger.Error(msg, e);
                throw new ACBrDFeException(msg, e);
            }
        }