diff --git a/CHANGES.md b/CHANGES.md index f3934328..0fbf46bd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ Changelog In Development -------------- +- Risolo problema deserializzazione Json per la classe `Allegati` ([#344][344]) + +[344]: https://github.com/FatturaElettronica/FatturaElettronica.NET/pull/344 + v 3.1.4 ------- diff --git a/Core/BaseClassSerializable.cs b/Core/BaseClassSerializable.cs index ef07d8e1..cad48fed 100644 --- a/Core/BaseClassSerializable.cs +++ b/Core/BaseClassSerializable.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Linq; using System.Reflection; +using System.Text; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; @@ -23,7 +24,7 @@ public class BaseClassSerializable : BaseClass, IXmlSerializable /// protected BaseClassSerializable() { - XmlOptions = new XmlOptions {DateTimeFormat = "yyyy-MM-dd", DecimalFormat = "0.00######"}; + XmlOptions = new XmlOptions { DateTimeFormat = "yyyy-MM-dd", DecimalFormat = "0.00######" }; } protected BaseClassSerializable(XmlReader r) @@ -78,7 +79,7 @@ public virtual void FromJson(JsonReader r) try { - if (add != null) add.Invoke(current.Value, new[] {newObject}); + if (add != null) add.Invoke(current.Value, new[] { newObject }); } catch (Exception) { @@ -123,8 +124,8 @@ public virtual void FromJson(JsonReader r) current = _stack.Peek(); - var name = (string) r.Value; - prop = GetPropertyInfo((BaseClassSerializable) current.Value, name); + var name = (string)r.Value; + prop = GetPropertyInfo((BaseClassSerializable)current.Value, name); if (prop == null) throw new JsonParseException($"Unexpected property {name}", r); @@ -194,7 +195,7 @@ public virtual void FromJson(JsonReader r) try { - if (add != null) add.Invoke(current.Value, new[] {value}); + if (add != null) add.Invoke(current.Value, new[] { value }); } catch (Exception) { @@ -231,6 +232,9 @@ private static object Cast(Type target, object value) if (target == typeof(decimal) || target == typeof(decimal?)) return Convert.ToDecimal(value); + if (target == typeof(byte[]) && value.GetType().Equals(String.Empty.GetType())) + return Convert.FromBase64String((String)value); + return value; } @@ -251,7 +255,7 @@ private static PropertyInfo GetPropertyInfo(BaseClassSerializable value, string // XmlElementAttribute comes first property = dataProperties .FirstOrDefault(prop => prop.GetCustomAttributes(typeof(XmlElementAttribute), false) - .Any(ca => ((XmlElementAttribute) ca).ElementName.Equals(name, + .Any(ca => ((XmlElementAttribute)ca).ElementName.Equals(name, StringComparison.OrdinalIgnoreCase))); // Fallback to property name @@ -330,14 +334,14 @@ public virtual void WriteXml(XmlWriter w) switch (value) { case string _: - { - if (!string.IsNullOrEmpty(value.ToString()) || XmlOptions.SerializeEmptyStrings) { - w.WriteElementString(propertyName, value.ToString()); - } + if (!string.IsNullOrEmpty(value.ToString()) || XmlOptions.SerializeEmptyStrings) + { + w.WriteElementString(propertyName, value.ToString()); + } - continue; - } + continue; + } case DateTime dateTime when XmlOptions.DateTimeFormat != null && !property.GetCustomAttributes().Any(): w.WriteElementString(propertyName, dateTime.ToString(XmlOptions.DateTimeFormat)); @@ -418,7 +422,7 @@ public virtual void ReadXml(XmlReader r) .FirstOrDefault(prop => prop .GetCustomAttributes(typeof(XmlElementAttribute), false) - .Any(ca => ((XmlElementAttribute) ca).ElementName == r.Name)); + .Any(ca => ((XmlElementAttribute)ca).ElementName == r.Name)); if (property == null) property = properties.FirstOrDefault(n => n.Name.Equals(r.Name)); if (property == null) @@ -432,7 +436,7 @@ public virtual void ReadXml(XmlReader r) if (type.IsSubclassOfBusinessObject()) { - ((BaseClassSerializable) value).ReadXml(r); + ((BaseClassSerializable)value).ReadXml(r); continue; } @@ -480,19 +484,19 @@ private static void ReadXmlList(object propertyValue, Type propertyType, string { // list items are expected to be of BusinessObject type. var bo = Activator.CreateInstance(argumentType); - ((BaseClassSerializable) bo).ReadXml(r); - if (add != null) add.Invoke(propertyValue, new[] {bo}); + ((BaseClassSerializable)bo).ReadXml(r); + if (add != null) add.Invoke(propertyValue, new[] { bo }); continue; } if (argumentType == typeof(string)) { - if (add != null) add.Invoke(propertyValue, new object[] {r.ReadElementContentAsString()}); + if (add != null) add.Invoke(propertyValue, new object[] { r.ReadElementContentAsString() }); continue; } if (argumentType != typeof(int)) continue; - if (add != null) add.Invoke(propertyValue, new[] {r.ReadElementContentAs(argumentType, null)}); + if (add != null) add.Invoke(propertyValue, new[] { r.ReadElementContentAs(argumentType, null) }); } } diff --git a/Defaults.cs b/Defaults.cs index a354516b..5d9928fc 100644 --- a/Defaults.cs +++ b/Defaults.cs @@ -14,16 +14,20 @@ public static class FormatoTrasmissione public static string Semplificata { get { return $"FSM{Versione.TrasmissioneSemplificata.Replace(".", "")}"; } } } - public class RootElement + public class RootElement { - public static string Prefix { get; set; } = "p"; - - public static XmlAttributeString[] ExtraAttributes { get; set; } = new XmlAttributeString[] { - new XmlAttributeString { Prefix="xmlns", LocalName="ds", ns=null, value="http://www.w3.org/2000/09/xmldsig#"}, - new XmlAttributeString { Prefix="xmlns", LocalName="xsi", ns=null, value="http://www.w3.org/2001/XMLSchema-instance"}, - new XmlAttributeString { Prefix="xsi", LocalName="schemaLocation", ns=null, value="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1a.xsd"} - }; - + public static string Prefix { get { return "p"; } } + public static XmlAttributeString[] ExtraAttributes + { + get + { + return new XmlAttributeString[] { + new XmlAttributeString { Prefix="xmlns", LocalName="ds", ns=null, value="http://www.w3.org/2000/09/xmldsig#"}, + new XmlAttributeString { Prefix="xmlns", LocalName="xsi", ns=null, value="http://www.w3.org/2001/XMLSchema-instance"}, + new XmlAttributeString { Prefix="xsi", LocalName="schemaLocation", ns=null, value="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1a.xsd"} + }; + } + } public class XmlAttributeString { public string Prefix; @@ -33,4 +37,4 @@ public class XmlAttributeString } } -} +} \ No newline at end of file diff --git a/Test/Core/JsonTest.cs b/Test/Core/JsonTest.cs index 3ae2c7a9..458d5672 100644 --- a/Test/Core/JsonTest.cs +++ b/Test/Core/JsonTest.cs @@ -11,10 +11,12 @@ public class JsonTest [TestMethod] public void JsonDeSerialize() { - var original = new TestMe { AString = "a string", ADate = DateTime.Now, ADecimal = 0.12345678m }; + var base64String = "VGhpcyBpcyBhIHN0cmluZw=="; + var original = new TestMe { AString = "a string", ADate = DateTime.Now, ADecimal = 0.12345678m, AByteArray = Convert.FromBase64String(base64String) }; original.SubTestMe.AString = "a sub string"; original.SubTestMe.ADate = DateTime.Now.AddDays(+1); original.SubTestMe.ADecimal = 0.98765432m; + original.SubTestMe.AByteArray = Convert.FromBase64String(base64String); var json = original.ToJson(); Assert.IsFalse(json.Contains("XmlOptions")); @@ -25,9 +27,11 @@ public void JsonDeSerialize() Assert.AreEqual(original.AString, challenge.AString); Assert.AreEqual(original.ADate, challenge.ADate); Assert.AreEqual(original.ADecimal, challenge.ADecimal); + CollectionAssert.AreEqual(original.AByteArray, challenge.AByteArray); Assert.AreEqual(original.SubTestMe.AString, challenge.SubTestMe.AString); Assert.AreEqual(original.SubTestMe.ADate, challenge.SubTestMe.ADate); Assert.AreEqual(original.SubTestMe.ADecimal, challenge.SubTestMe.ADecimal); + CollectionAssert.AreEqual(original.SubTestMe.AByteArray, challenge.SubTestMe.AByteArray); } } } diff --git a/Test/Core/TestMe.cs b/Test/Core/TestMe.cs index 10030e6d..9ba58fa2 100644 --- a/Test/Core/TestMe.cs +++ b/Test/Core/TestMe.cs @@ -12,6 +12,8 @@ public class TestMe : BaseClassSerializable [DataProperty] public decimal ADecimal { get; set; } [DataProperty] + public byte[] AByteArray { get; set; } + [DataProperty] public SubTestMe SubTestMe { get; } = new SubTestMe(); public override void WriteXml(System.Xml.XmlWriter w) { @@ -28,5 +30,7 @@ public class SubTestMe : BaseClassSerializable public DateTime ADate { get; set; } [DataProperty] public decimal ADecimal { get; set; } + [DataProperty] + public byte[] AByteArray { get; set; } } }