Skip to content

Commit

Permalink
Merge pull request #46 from sharpcode-it/engineering87-develop
Browse files Browse the repository at this point in the history
Migrate from Newtonsoft.Json to System.Text.Json
  • Loading branch information
engineering87 authored Aug 28, 2024
2 parents 9e848ed + deff775 commit 803338f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -16,7 +16,7 @@ public static class ObjectSerializationHelper
/// <returns></returns>
public static string SerializeToJson(this object istance)
{
return istance == null ? string.Empty : JsonConvert.SerializeObject(istance);
return istance == null ? string.Empty : JsonSerializer.Serialize(istance);
}

/// <summary>
Expand All @@ -27,7 +27,7 @@ public static string SerializeToJson(this object istance)
/// <returns></returns>
public static T DeserializeFromJson<T>(this string istance) where T : class
{
return string.IsNullOrEmpty(istance) ? default : JsonConvert.DeserializeObject<T>(istance);
return string.IsNullOrEmpty(istance) ? default : JsonSerializer.Deserialize<T>(istance);
}

/// <summary>
Expand Down
7 changes: 4 additions & 3 deletions SharpHelpers/SharpHelpers/SharpHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
<PackageIcon>ico.png</PackageIcon>
<PackageIconUrl />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
Expand All @@ -38,4 +35,8 @@
<ItemGroup>
<Compile Remove="RegistryHelpers.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
</Project>
11 changes: 8 additions & 3 deletions SharpHelpers/SharpHelpers/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -223,8 +223,13 @@ public static string Right(this string str, int length)
/// <param name="strJson"></param>
public static T JsonToObject<T>(this string strJson)
{
return JsonConvert.DeserializeObject<T>(strJson,
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
var options = new JsonSerializerOptions
{
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles,
PropertyNameCaseInsensitive = true
};

return JsonSerializer.Deserialize<T>(strJson, options);
}

/// <summary>
Expand Down

0 comments on commit 803338f

Please sign in to comment.