Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Newtonsoft dependency removed - close #12 #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure as to whether this should be case sensitive or insensitive. The original intent of the .NET port was to match the behavior of the Java implementation, so perhaps we should try to match what it does by default.

@cer: Do you know if the Java eventuate-tram is case-sensitive or insensitive for property names during deserialization?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};

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);
}
}
}