public static XObject[] SerializeChild(ICollection values, DFeCollectionAttribute tag, DFeItemAttribute[] itemTags, SerializerOptions options)
        {
            XElement arrayElement = null;

            if (tag.IsValue)
            {
                arrayElement = new XElement(tag.Name);
            }

            var childElements = 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.");

                XElement childElement;
                if (itemTag.IsValue)
                {
                    var properties = value.GetType().GetPropsAndValidate(value);
                    var valueProp  = properties.SingleOrDefault(x => x.HasAttribute <DFeItemValueAttribute>());
                    var valueAtt   = valueProp.GetAttribute <DFeItemValueAttribute>();

                    XNamespace aw = itemTag.Namespace ?? string.Empty;
                    childElement = new XElement(aw + itemTag.Name);

                    var childValue = valueProp.GetValueOrIndex(value);
                    var estaVazio  = childValue == null || childValue.ToString().IsEmpty();
                    childElement.Value = PrimitiveSerializer.ProcessValue(ref estaVazio, valueAtt.Tipo, valueProp,
                                                                          valueAtt.Ocorrencia, valueAtt.Min, valueProp, value);

                    foreach (var property in properties.Where(x => x.HasAttribute <DFeAttributeAttribute>()))
                    {
                        var attTag = property.GetAttribute <DFeAttributeAttribute>();
                        var att    = (XAttribute)PrimitiveSerializer.Serialize(attTag, value, property, options);
                        childElement.AddAttribute(att);
                    }
                }
                else
                {
                    childElement = ObjectSerializer.Serialize(value, value.GetType(), itemTag.Name, itemTag.Namespace, options);
                }

                childElements.Add(childElement);
            }

            if (!tag.IsValue)
            {
                arrayElement.AddChild(childElements.ToArray());
            }

            return(tag.IsValue ? childElements.ToArray() : new XObject[] { arrayElement });
        }
Beispiel #2
0
        public static XObject[] SerializeChild(ICollection values, DFeCollectionAttribute tag, DFeItemAttribute[] itemTags, SerializerOptions options)
        {
            var arrayElement = new XElement(tag.Name);

            foreach (var value in values)
            {
                var itemTag      = itemTags.SingleOrDefault(x => x.Tipo == value.GetType());
                var childElement = ObjectSerializer.Serialize(value, value.GetType(), itemTag?.Name, options);
                arrayElement.AddChild(childElement);
            }

            return(new XObject[] { arrayElement });
        }
        public static XObject[] SerializeChild(ICollection values, DFeCollectionAttribute tag, DFeItemAttribute[] itemTags, SerializerOptions options)
        {
            var arrayElement = new XElement(tag.Name);

            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.");

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

                    Guard.Against <ACBrDFeException>(!properties.All(x => x.HasAttribute <DFeItemValueAttribute>() || x.HasAttribute <DFeAttributeAttribute>()),
                                                     $"Item {value.GetType().Name} é do tipo [ItemValue] e so pode ter atributo do tipo [DFeAttributeAttribute] ou [DFeItemValueAttribute].");

                    Guard.Against <ACBrDFeException>(properties.Count(x => x.HasAttribute <DFeItemValueAttribute>()) != 1,
                                                     $"Item {value.GetType().Name} é do tipo [ItemValue] e não tem presente o atributo [DFeItemValueAttribute] ou possui mais de um atributo.");

                    var valueProp = properties.SingleOrDefault(x => x.HasAttribute <DFeItemValueAttribute>());
                    var valueAtt  = valueProp.GetAttribute <DFeItemValueAttribute>();

                    XNamespace aw = itemTag.Namespace ?? string.Empty;
                    childElement = new XElement(aw + itemTag.Name);

                    var childValue = valueProp.GetValueOrIndex(value);
                    var estaVazio  = childValue == null || childValue.ToString().IsEmpty();
                    childElement.Value = PrimitiveSerializer.ProcessValue(ref estaVazio, valueAtt.Tipo, valueProp,
                                                                          valueAtt.Ocorrencia, valueAtt.Min, valueProp, value);

                    foreach (var property in properties.Where(x => x.HasAttribute <DFeAttributeAttribute>()))
                    {
                        var attTag = property.GetAttribute <DFeAttributeAttribute>();
                        var att    = (XAttribute)PrimitiveSerializer.Serialize(attTag, value, property, options);
                        childElement.AddAttribute(att);
                    }
                }
                else
                {
                    childElement = ObjectSerializer.Serialize(value, value.GetType(), itemTag.Name, itemTag.Namespace, options);
                }

                arrayElement.AddChild(childElement);
            }

            return(new XObject[] { arrayElement });
        }
Beispiel #4
0
        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()) ?? attributes[0];

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

            return(new XObject[] { ObjectSerializer.Serialize(value, value.GetType(), itemAttribute.Name, options) });
        }
Beispiel #5
0
        public static XElement[] SerializeChild(ICollection values, DFeCollectionAttribute tag, DFeItemAttribute[] itemTags, SerializerOptions options)
        {
            var childElements = 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 childElement = ObjectSerializer.Serialize(value, value.GetType(), itemTag.Name, itemTag.Namespace, options);
                childElements.Add(childElement);
            }

            return(childElements.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(new XObject[] { ObjectSerializer.Serialize(value, value.GetType(), itemAttribute.Name, itemAttribute.Namespace, options) });
        }
Beispiel #7
0
        private XDocument Serialize(object item)
        {
            var xmldoc = Options.OmitirDeclaracao ? new XDocument() : new XDocument(new XDeclaration("1.0", "UTF-8", null));

            var rooTag = tipoDFe.GetAttribute <DFeRootAttribute>();

            var rootName = rooTag.Name;

            if (rootName.IsEmpty())
            {
                var root = tipoDFe.GetRootName(item);
                rootName = root.IsEmpty() ? tipoDFe.Name : root;
            }

            var rootElement = ObjectSerializer.Serialize(item, tipoDFe, rootName, rooTag.Namespace, Options);

            xmldoc.Add(rootElement);
            xmldoc.RemoveEmptyNs();

            return(xmldoc);
        }
 public static XObject[] SerializeObjects(ICollection values, DFeItemAttribute tag, SerializerOptions options)
 {
     return((from object value in values select ObjectSerializer.Serialize(value, value.GetType(), tag.Name, tag.Namespace, options)).Cast <XObject>().ToArray());
 }
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryAttribute)}]");
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryKeyAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryKeyAttribute)}]");
            Guard.Against <ACBrDFeException>(!prop.HasAttribute <DFeDictionaryValueAttribute>(), $"Atributo necessário não encontrado [{nameof(DFeDictionaryValueAttribute)}]");

            var tag      = prop.GetAttribute <DFeDictionaryAttribute>();
            var keyAtt   = prop.GetAttribute <DFeDictionaryKeyAttribute>();
            var valueAtt = prop.GetAttribute <DFeDictionaryValueAttribute>();

            Guard.Against <ArgumentNullException>(!keyAtt.AsAttribute && tag.ItemName.IsEmpty(), "Se a Key não é um atributo é necessario informar o [ItemName]");

            var dictionary = (IDictionary)prop.GetValue(parentObject, null);

            if (dictionary.Count < tag.MinSize || dictionary.Count > tag.MaxSize && tag.MaxSize > 0)
            {
                var msg = dictionary.Count > tag.MaxSize ? DFeSerializer.ErrMsgMaiorMaximo : DFeSerializer.ErrMsgMenorMinimo;
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, msg);
            }

            if (dictionary.Count == 0 && tag.MinSize == 0 && tag.Ocorrencia == Ocorrencia.NaoObrigatoria)
            {
                return(null);
            }

            var args = dictionary.GetType().GetGenericArguments();

            Guard.Against <ArgumentException>(args.Length != 2);

            var keyType   = ObjectType.From(args[0]);
            var valueType = ObjectType.From(args[1]);

            Guard.Against <ACBrDFeException>(keyType != ObjectType.PrimitiveType && keyAtt.AsAttribute);

            var list = new List <XElement>();

            var dicENumerator = dictionary.GetEnumerator();

            while (dicENumerator.MoveNext())
            {
                var key   = dicENumerator.Entry.Key;
                var value = dicENumerator.Entry.Value;

                var keyElement = keyType == ObjectType.PrimitiveType
                    ? PrimitiveSerializer.Serialize(keyAtt, key, options)
                    : ObjectSerializer.Serialize(key, key.GetType(), keyAtt.Name, keyAtt.Namespace, options);

                var valueElement = valueType == ObjectType.PrimitiveType
                    ? (XElement)PrimitiveSerializer.Serialize(valueAtt, value, options)
                    : ObjectSerializer.Serialize(value, value.GetType(), valueAtt.Name, valueAtt.Namespace, options);

                if (keyAtt.AsAttribute)
                {
                    valueElement.AddAttribute((XAttribute)keyElement);
                    list.Add(valueElement);
                }
                else
                {
                    var itemElement = new XElement(tag.ItemName);
                    itemElement.AddChild((XElement)keyElement);
                    itemElement.AddChild(valueElement);

                    list.Add(itemElement);
                }
            }

            var element = new XElement(tag.Name, tag.Namespace);

            element.AddChild(list.ToArray());

            return(new XObject[] { element });
        }