Skip to content

Commit

Permalink
refactor: Newtonsoft dependency removed - close eventuate-tram#12
Browse files Browse the repository at this point in the history
  • Loading branch information
cetin.dogu committed Jun 7, 2024
1 parent 4e3bead commit 832b45e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
1 change: 0 additions & 1 deletion IO.Eventuate.Tram/IO.Eventuate.Tram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
59 changes: 28 additions & 31 deletions IO.Eventuate.Tram/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(string json)
{
return JsonConvert.DeserializeObject<T>(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<T>(string json)
{
return JsonSerializer.Deserialize<T>(json, JsonSerializerOptions);
}

public static object FromJson(string json, Type type)
{
return JsonSerializer.Deserialize(json, type, JsonSerializerOptions);
}
}
}

0 comments on commit 832b45e

Please sign in to comment.