From 832b45e210ba271e5b72716cd7ffa3b79539ec60 Mon Sep 17 00:00:00 2001 From: "cetin.dogu" Date: Fri, 7 Jun 2024 17:17:11 +0300 Subject: [PATCH] refactor: Newtonsoft dependency removed - close #12 --- IO.Eventuate.Tram/IO.Eventuate.Tram.csproj | 1 - IO.Eventuate.Tram/JsonMapper.cs | 59 ++++++++++------------ 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/IO.Eventuate.Tram/IO.Eventuate.Tram.csproj b/IO.Eventuate.Tram/IO.Eventuate.Tram.csproj index a22d393..d5ce0b5 100644 --- a/IO.Eventuate.Tram/IO.Eventuate.Tram.csproj +++ b/IO.Eventuate.Tram/IO.Eventuate.Tram.csproj @@ -11,7 +11,6 @@ - diff --git a/IO.Eventuate.Tram/JsonMapper.cs b/IO.Eventuate.Tram/JsonMapper.cs index 90a81b0..6b1e4de 100644 --- a/IO.Eventuate.Tram/JsonMapper.cs +++ b/IO.Eventuate.Tram/JsonMapper.cs @@ -6,39 +6,36 @@ */ using System; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; +using System.Text.Json; +using System.Text.Json.Serialization; namespace IO.Eventuate.Tram { - public static class JsonMapper - { - public static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings - { - // Don't use CamelCasePropertyNamesContractResolver - // The CamelCasePropertyNamesContractResolver uses an internal cache that is shared between instances, - // so sometimes dictionary keys were getting camel cased if the wrong settings got cached. - ContractResolver = new DefaultContractResolver - { - NamingStrategy = new CamelCaseNamingStrategy() - }, - MissingMemberHandling = MissingMemberHandling.Ignore, - NullValueHandling = NullValueHandling.Ignore - }; - - public static string ToJson(object o) - { - return JsonConvert.SerializeObject(o, JsonSerializerSettings); - } + public static class JsonMapper + { + public static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions + { + // Use camel case for property names + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + // Ignore null values during serialization + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + // Make property name matching case-insensitive during deserialization + PropertyNameCaseInsensitive = true + }; - public static T FromJson(string json) - { - return JsonConvert.DeserializeObject(json, JsonSerializerSettings); - } - - public static object FromJson(string json, Type type) - { - return JsonConvert.DeserializeObject(json, type, JsonSerializerSettings); - } - } + public static string ToJson(object o) + { + return JsonSerializer.Serialize(o, JsonSerializerOptions); + } + + public static T FromJson(string json) + { + return JsonSerializer.Deserialize(json, JsonSerializerOptions); + } + + public static object FromJson(string json, Type type) + { + return JsonSerializer.Deserialize(json, type, JsonSerializerOptions); + } + } } \ No newline at end of file