Beispiel #1
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[] 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 #3
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 <VipException>(itemTag == null, $"Item {value.GetType().Name} não presente na lista de itens.");

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

                    Guard.Against <VipException>(!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 <VipException>(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().IsNullOrEmpty();
                    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 #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[] SerializePrimitive(PropertyInfo prop, object parentObject, ICollection values, DFeCollectionAttribute tag,
                                                   SerializerOptions options)
        {
            var retElements = new List <XObject>();

            for (var i = 0; i < values.Count; i++)
            {
                var ret = PrimitiveSerializer.Serialize(tag, parentObject, prop, options, i);
                retElements.Add(ret);
            }

            return(retElements.ToArray());
        }
 public static XObject[] SerializeObjects(ICollection values, DFeCollectionAttribute tag, SerializerOptions options)
 {
     return((from object value in values select ObjectSerializer.Serialize(value, value.GetType(), tag.Name, tag.Namespace, options)).Cast <XObject>()
            .ToArray());
 }