Skip to content

Commit

Permalink
Merge branch 'pull#344'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaiarocci committed May 14, 2021
2 parents 64b101f + 10a5bf8 commit 179b6b3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------

Expand Down
40 changes: 22 additions & 18 deletions Core/BaseClassSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +24,7 @@ public class BaseClassSerializable : BaseClass, IXmlSerializable
/// </summary>
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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -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<IgnoreXmlDateFormat>().Any():
w.WriteElementString(propertyName, dateTime.ToString(XmlOptions.DateTimeFormat));
Expand Down Expand Up @@ -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)
Expand All @@ -432,7 +436,7 @@ public virtual void ReadXml(XmlReader r)

if (type.IsSubclassOfBusinessObject())
{
((BaseClassSerializable) value).ReadXml(r);
((BaseClassSerializable)value).ReadXml(r);
continue;
}

Expand Down Expand Up @@ -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) });
}
}

Expand Down
24 changes: 14 additions & 10 deletions Defaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,4 +37,4 @@ public class XmlAttributeString
}
}

}
}
6 changes: 5 additions & 1 deletion Test/Core/JsonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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);
}
}
}
4 changes: 4 additions & 0 deletions Test/Core/TestMe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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; }
}
}

0 comments on commit 179b6b3

Please sign in to comment.