Example #1
0
        /// <summary>
        /// Deserializes the XElement to the fundamental primitive (e.g. string, int etc.) of a specified type using options.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="parentElement">The parent XElement used to deserialize the fundamental primitive.</param>
        /// <param name="item">The item.</param>
        /// <param name="prop">The property.</param>
        /// <param name="options">The options.</param>
        /// <returns>The deserialized fundamental primitive from the XElement.</returns>
        public static object Deserialize(DFeBaseAttribute tag, XObject parentElement, object item, PropertyInfo prop, SerializerOptions options, int idx = -1)
        {
            if (parentElement == null)
            {
                return(null);
            }

            var element = parentElement as XElement;
            var value   = element?.Value ?? ((XAttribute)parentElement).Value;

            return(GetValue(tag.Tipo, value, item, prop));
        }
Example #2
0
 public static XObject Serialize(DFeBaseAttribute tag, object value, SerializerOptions options)
 {
     try
     {
         var estaVazio          = value == null || value.ToString().IsEmpty();
         var conteudoProcessado = ProcessValue(ref estaVazio, tag.Tipo, value, tag.Ocorrencia, tag.Min, null, null);
         return(ProcessContent(tag, conteudoProcessado, estaVazio, options));
     }
     catch (Exception ex)
     {
         options.AddAlerta(tag.Id, tag.Name, tag.Descricao, ex.ToString());
         return(null);
     }
 }
Example #3
0
        /// <summary>
        /// Serializes a fundamental primitive object (e.g. string, int etc.) into a XElement using options.
        /// </summary>
        /// <param name="tag">The name of the primitive to serialize.</param>
        /// <param name="item">The item.</param>
        /// <param name="prop">The property.</param>
        /// <param name="options">Indicates how the output is formatted or serialized.</param>
        /// <param name="idx"></param>
        /// <returns>The XElement representation of the primitive.</returns>
        public static XObject Serialize(DFeBaseAttribute tag, object item, PropertyInfo prop, SerializerOptions options, int idx = -1)
        {
            try
            {
                var value              = prop.GetValueOrIndex(item, idx);
                var estaVazio          = value == null || value.ToString().IsEmpty();
                var conteudoProcessado = ProcessValue(ref estaVazio, tag.Tipo, value, tag.Ocorrencia, tag.Min, prop, item);

                return(ProcessContent(tag, conteudoProcessado, estaVazio, options));
            }
            catch (Exception ex)
            {
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, ex.ToString());
                return(null);
            }
        }
Example #4
0
        private static XObject ProcessContent(DFeBaseAttribute tag, string conteudoProcessado, bool estaVazio, SerializerOptions options)
        {
            string alerta;

            if (tag.Ocorrencia == Ocorrencia.Obrigatoria && estaVazio && tag.Min > 0)
            {
                alerta = DFeSerializer.ErrMsgVazio;
            }
            else
            {
                alerta = string.Empty;
            }

            if (conteudoProcessado.IsEmpty() && conteudoProcessado.Length < tag.Min && alerta.IsEmpty() && conteudoProcessado.Length > 1)
            {
                alerta = DFeSerializer.ErrMsgMenor;
            }

            if (!string.IsNullOrEmpty(conteudoProcessado.Trim()) && conteudoProcessado.Length > tag.Max)
            {
                alerta = DFeSerializer.ErrMsgMaior;
            }

            if (!string.IsNullOrEmpty(alerta.Trim()) && DFeSerializer.ErrMsgVazio.Equals(alerta) && !estaVazio)
            {
                alerta += $" [{tag.Name}]";
            }

            options.AddAlerta(tag.Id, tag.Name, tag.Descricao, alerta);

            XObject xmlTag = null;

            if (tag.Ocorrencia == Ocorrencia.Obrigatoria && estaVazio)
            {
                xmlTag = tag is DFeElementAttribute ? (XObject) new XElement(tag.Name) : new XAttribute(tag.Name, "");
            }

            if (estaVazio)
            {
                return(xmlTag);
            }

            var elementValue = options.RemoverAcentos ? conteudoProcessado.RemoveAccent() : conteudoProcessado;

            elementValue = options.RemoverEspacos ? elementValue.Trim() : elementValue;

            switch (tag)
            {
            case DFeAttributeAttribute _: return(new XAttribute(tag.Name, elementValue));

            case DFeDictionaryKeyAttribute keyAtt when keyAtt.AsAttribute: return(new XAttribute(keyAtt.Name, elementValue));

            case DFeElementAttribute elementAtt:
                if (elementValue.IsCData())
                {
                    elementValue = elementValue.RemoveCData();
                    return(new XElement(tag.Name, new XCData(elementValue)));
                }
                else
                {
                    return(elementAtt.UseCData ? new XElement(tag.Name, new XCData(elementValue)) :
                           new XElement(tag.Name, elementValue));
                }

            default:
                return(new XElement(tag.Name, elementValue));
            }
        }