Skip to content

Commit

Permalink
Revert "Migrazione a System.Text.Json'"
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Mar 3, 2022
1 parent b41834d commit 36727be
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 161 deletions.
1 change: 0 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Collaboratori
- Luca Borghini
- Luca Marcato
- Marco Checchin
- Marco Saltarelli
- Marco Tessitore
- Massimo Linossi
- Michael Mairegger
Expand Down
2 changes: 0 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ Released on February 25, 2022

- New: supporto per tag 'Processing Instructions' in de-serializzazione XML ([#367][367])
- Fix: link rotto alle specifiche techiche ([#362][362])
- Migrazione a System.Text.Json ([#360][360])
- Rimosso badge dependabot dal README

[367]: https://github.com/FatturaElettronica/FatturaElettronica.NET/pull/367
[360]: https://github.com/FatturaElettronica/FatturaElettronica.NET/issues/360
[362]: https://github.com/FatturaElettronica/FatturaElettronica.NET/issues/362

## Released
Expand Down
4 changes: 2 additions & 2 deletions src/FatturaElettronica/Common/DenominazioneNomeCognome.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Text.Json.Serialization;
using System.Xml;
using System.Xml;
using Newtonsoft.Json;

namespace FatturaElettronica.Common
{
Expand Down
132 changes: 56 additions & 76 deletions src/FatturaElettronica/Core/BaseClassSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Formatting = Newtonsoft.Json.Formatting;

namespace FatturaElettronica.Core
{
Expand Down Expand Up @@ -39,10 +40,12 @@ protected BaseClassSerializable(XmlReader r)
/// </summary>
/// <param name="r">Active json stream reader</param>
/// <remarks>Side effects on parse handling</remarks>
public virtual void FromJson(Utf8JsonReader r)
public virtual void FromJson(JsonReader r)
{
_stack.Clear();

r.FloatParseHandling = FloatParseHandling.Decimal;

PropertyInfo prop = null;

while (r.Read())
Expand All @@ -52,7 +55,7 @@ public virtual void FromJson(Utf8JsonReader r)
Type elementType;
switch (r.TokenType)
{
case JsonTokenType.StartObject:
case JsonToken.StartObject:

current = null;
if (_stack.Any())
Expand All @@ -67,7 +70,13 @@ public virtual void FromJson(Utf8JsonReader r)

if (objectType.IsGenericList())
{
elementType = objectType.GetTypeInfo().GenericTypeArguments.Single();
elementType = objectType.GetTypeInfo()
#if NET35
.GetGenericArguments()
#else
.GenericTypeArguments
#endif
.Single();

var newObject = Activator.CreateInstance(elementType);

Expand All @@ -82,6 +91,7 @@ public virtual void FromJson(Utf8JsonReader r)
throw new JsonParseException($"Unexpected element type {elementType}", r);
}


current = new(newObject);
}
else
Expand All @@ -101,8 +111,7 @@ public virtual void FromJson(Utf8JsonReader r)
_stack.Push(current);

break;

case JsonTokenType.EndObject:
case JsonToken.EndObject:

if (_stack.Count > 0)
_stack.Pop();
Expand All @@ -113,15 +122,14 @@ public virtual void FromJson(Utf8JsonReader r)
prop = _stack.ElementAt(1).Child;

break;

case JsonTokenType.PropertyName:
case JsonToken.PropertyName:

if (!_stack.Any())
throw new JsonParseException("Malformed JSON", r);

current = _stack.Peek();

var name = r.GetString();
var name = (string)r.Value;
prop = GetPropertyInfo((BaseClassSerializable)current.Value, name);

if (prop == null)
Expand All @@ -130,8 +138,7 @@ public virtual void FromJson(Utf8JsonReader r)
current.Child = prop;

break;

case JsonTokenType.StartArray:
case JsonToken.StartArray:

current = null;
if (_stack.Any())
Expand All @@ -158,7 +165,7 @@ public virtual void FromJson(Utf8JsonReader r)

break;

case JsonTokenType.EndArray:
case JsonToken.EndArray:

if (_stack.Count > 0)
_stack.Pop();
Expand All @@ -169,11 +176,13 @@ public virtual void FromJson(Utf8JsonReader r)
prop = _stack.ElementAt(1).Child;

break;
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.String:
case JsonToken.Boolean:
case JsonToken.Null:
case JsonToken.Date:

case JsonTokenType.Number:
case JsonTokenType.String:
case JsonTokenType.True:
case JsonTokenType.False:
current = null;
if (_stack.Any())
current = _stack.Peek();
Expand All @@ -184,10 +193,16 @@ public virtual void FromJson(Utf8JsonReader r)

if (current.Value.GetType().IsGenericList())
{
elementType = objectType.GetTypeInfo().GenericTypeArguments.Single();
elementType = objectType.GetTypeInfo()
#if NET35
.GetGenericArguments()
#else
.GenericTypeArguments
#endif
.Single();

var add = objectType?.GetMethod("Add");
var value = Cast(elementType, r);
var value = Cast(elementType, r.Value);

try
{
Expand All @@ -200,48 +215,14 @@ public virtual void FromJson(Utf8JsonReader r)
}
else
{
var value = Cast(objectType, r);
var value = Cast(objectType, r.Value);
current.Child.SetValue(current.Value, value, null);
}
}
else
throw new JsonParseException("Malformed JSON", r);
break;

case JsonTokenType.Null:

current = null;
if (_stack.Any())
current = _stack.Peek();

if (current != null)
{
objectType = prop?.PropertyType;

if (current.Value.GetType().IsGenericList())
{
elementType = objectType.GetTypeInfo().GenericTypeArguments.Single();

var add = objectType?.GetMethod("Add");

try
{
if (add != null) add.Invoke(current.Value, null);
}
catch (Exception)
{
throw new JsonParseException($"Unexpected element type {elementType}", r);
}
}
else
{
current.Child.SetValue(current.Value, null, null);
}
}
else
throw new JsonParseException("Malformed JSON", r);
break;

default:
throw new JsonParseException($"Unexpected token {r.TokenType}", r);
}
Expand All @@ -252,29 +233,20 @@ public virtual void FromJson(Utf8JsonReader r)
/// Helper method to cast json properties
/// </summary>
/// <param name="target">target type</param>
/// <param name="r">Active json stream reader</param>
/// <param name="value">source value</param>
/// <returns></returns>
private static object Cast(Type target, Utf8JsonReader r)
private static object Cast(Type target, object value)
{
if (target == typeof(int) || target == typeof(int?))
return r.GetInt32();
return Convert.ToInt32(value);

if (target == typeof(decimal) || target == typeof(decimal?))
return r.GetDecimal();

if (target == typeof(DateTime) || target == typeof(DateTime?))
return r.GetDateTime();
return Convert.ToDecimal(value);

if (target == typeof(string))
return r.GetString();
if (target == typeof(byte[]) && value.GetType().Equals(String.Empty.GetType()))
return Convert.FromBase64String((String)value);

if (target == typeof(bool))
return r.GetBoolean();

if (target == typeof(byte[]))
return r.GetBytesFromBase64();

return null;
return value;
}

/// <summary>
Expand Down Expand Up @@ -320,7 +292,13 @@ public virtual string ToJson()
/// <returns>A JSON string representing the class instance.</returns>
public virtual string ToJson(JsonOptions jsonOptions)
{
var json = JsonSerializer.Serialize(this, GetType(), new JsonSerializerOptions { WriteIndented = jsonOptions == JsonOptions.Indented });
var json = JsonConvert.SerializeObject(
this, jsonOptions == JsonOptions.Indented ? Formatting.Indented : Formatting.None,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
DefaultValueHandling = DefaultValueHandling.Ignore
});
return json;
}

Expand Down Expand Up @@ -418,11 +396,9 @@ private static void WriteXmlList(string propertyName, object value, XmlWriter w)
case BaseClassSerializable serializable:
serializable.WriteXml(w);
break;

case string s:
w.WriteString(s);
break;

default:
w.WriteValue(e.Current);
break;
Expand Down Expand Up @@ -504,8 +480,13 @@ public virtual void ReadXml(XmlReader r)
/// <param name="r">Active XML stream reader.</param>
private static void ReadXmlList(object propertyValue, Type propertyType, string elementName, XmlReader r)
{
var argumentType = propertyType.GetTypeInfo().GenericTypeArguments.Single();

var argumentType = propertyType.GetTypeInfo()
#if NET35
.GetGenericArguments()
#else
.GenericTypeArguments
#endif
.Single();
// note that the 'canonical' call to GetRuntimeMethod returns null for some reason,
// see http://stackoverflow.com/questions/21307845/runtimereflectionextensions-getruntimemethod-does-not-work-as-expected
//var method = propertyType.GetRuntimeMethod("Clear", new[] { propertyType });
Expand Down Expand Up @@ -539,7 +520,6 @@ private static void ReadXmlList(object propertyValue, Type propertyType, string
private class JsonProperty
{
public PropertyInfo Child { get; set; }

public object Value { get; set; }

public JsonProperty(PropertyInfo property, object value)
Expand All @@ -558,4 +538,4 @@ public override string ToString()
}
}
}
}
}
19 changes: 8 additions & 11 deletions src/FatturaElettronica/Core/JsonParseException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Text.Json;

using System;
using Newtonsoft.Json;

namespace FatturaElettronica.Core
{
/// <summary>
Expand All @@ -9,16 +9,13 @@ namespace FatturaElettronica.Core
public class JsonParseException : Exception
{
private int LineNumber { get; }

private int LinePosition { get; }

public JsonParseException(string message, Utf8JsonReader reader) : base(message)
{
//Not available atm.
//As mentioned here https://github.com/dotnet/runtime/issues/28482 will be available from System.Text.Json vers.7.0.0

//LineNumber = textReader.LineNumber;
//LinePosition = textReader.textReader;
public JsonParseException(string message, JsonReader reader) : base(message)
{
if (!(reader is JsonTextReader textReader)) return;
LineNumber = textReader.LineNumber;
LinePosition = textReader.LinePosition;
}
}
}
24 changes: 12 additions & 12 deletions src/FatturaElettronica/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System.Text;
using System.Text.Json;

namespace FatturaElettronica.Extensions
{
public static class JsonExtensions
{
public static void FromJson(this FatturaBase fattura, string json)
{
fattura.FromJson(new Utf8JsonReader(Encoding.UTF8.GetBytes(json)));
}
}
using System.IO;
using Newtonsoft.Json;

namespace FatturaElettronica.Extensions
{
public static class JsonExtensions
{
public static void FromJson(this FatturaBase fattura, string json)
{
fattura.FromJson(new JsonTextReader(new StringReader(json)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
using System.Xml;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace FatturaElettronica.Ordinaria.FatturaElettronicaHeader
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public FatturaElettronicaHeader(XmlReader r) : base(r)
/// </summary>
[Core.DataProperty]
[XmlElement(ElementName = "RappresentanteFiscale")]
[JsonPropertyName("RappresentanteFiscale")]
[JsonProperty(PropertyName = "RappresentanteFiscale")]
public RappresentanteFiscale.RappresentanteFiscale Rappresentante { get; set; }

/// <summary>
Expand Down
Loading

0 comments on commit 36727be

Please sign in to comment.