From deff7754a86694123f976e5a6dd0176e8175ecf8 Mon Sep 17 00:00:00 2001 From: Francesco Del Re Date: Wed, 28 Aug 2024 11:50:17 +0200 Subject: [PATCH] Migrate from Newtonsoft.Json to System.Text.Json --- .../ObjectExtensions/ObjectSerializationhelper.cs | 6 +++--- SharpHelpers/SharpHelpers/SharpHelpers.csproj | 7 ++++--- SharpHelpers/SharpHelpers/StringHelper.cs | 11 ++++++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/SharpHelpers/SharpHelpers/ObjectExtensions/ObjectSerializationhelper.cs b/SharpHelpers/SharpHelpers/ObjectExtensions/ObjectSerializationhelper.cs index f7bcc05..f3a8c46 100755 --- a/SharpHelpers/SharpHelpers/ObjectExtensions/ObjectSerializationhelper.cs +++ b/SharpHelpers/SharpHelpers/ObjectExtensions/ObjectSerializationhelper.cs @@ -3,7 +3,7 @@ using System.IO; using System.Xml; using System.Xml.Serialization; -using Newtonsoft.Json; +using System.Text.Json; namespace SharpCoding.SharpHelpers.ObjectExtensions { @@ -16,7 +16,7 @@ public static class ObjectSerializationHelper /// public static string SerializeToJson(this object istance) { - return istance == null ? string.Empty : JsonConvert.SerializeObject(istance); + return istance == null ? string.Empty : JsonSerializer.Serialize(istance); } /// @@ -27,7 +27,7 @@ public static string SerializeToJson(this object istance) /// public static T DeserializeFromJson(this string istance) where T : class { - return string.IsNullOrEmpty(istance) ? default : JsonConvert.DeserializeObject(istance); + return string.IsNullOrEmpty(istance) ? default : JsonSerializer.Deserialize(istance); } /// diff --git a/SharpHelpers/SharpHelpers/SharpHelpers.csproj b/SharpHelpers/SharpHelpers/SharpHelpers.csproj index 2f0da91..473433f 100644 --- a/SharpHelpers/SharpHelpers/SharpHelpers.csproj +++ b/SharpHelpers/SharpHelpers/SharpHelpers.csproj @@ -20,9 +20,6 @@ ico.png - - - @@ -38,4 +35,8 @@ + + + + diff --git a/SharpHelpers/SharpHelpers/StringHelper.cs b/SharpHelpers/SharpHelpers/StringHelper.cs index 86adc4f..fb8fdf9 100644 --- a/SharpHelpers/SharpHelpers/StringHelper.cs +++ b/SharpHelpers/SharpHelpers/StringHelper.cs @@ -5,7 +5,7 @@ using System.Globalization; using System.Linq; using System.Text; -using Newtonsoft.Json; +using System.Text.Json; using System.Text.RegularExpressions; using SharpCoding.SharpHelpers.DomainModel; using SharpCoding.SharpHelpers.PrivateMethods; @@ -223,8 +223,13 @@ public static string Right(this string str, int length) /// public static T JsonToObject(this string strJson) { - return JsonConvert.DeserializeObject(strJson, - new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); + var options = new JsonSerializerOptions + { + ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles, + PropertyNameCaseInsensitive = true + }; + + return JsonSerializer.Deserialize(strJson, options); } ///