-
Notifications
You must be signed in to change notification settings - Fork 464
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
Replace Newtonsoft with System.Text.Json #364
Comments
Hi! It's actually 2-issues-in-1. I might be able to come to the upgrade to .NET 6 soon as it's trivial. On other hand, since it's trivial, please don't hesitate to contribute yourself. You can use #315 as a reference The latter is little less trivial, see #76. Meanwhile it's already possible to substitute the serialization engine by reimplementing IJsonSerializer. Again, it's trivial and if you could contribute that would be awesome. |
By looking at the list of supported target frameworks by the latest version, I see it supports |
@kristof12345 the former is done, please try version 8.8.1. For the latter, please consider contributing. I'd happy to help to get it reviewed and published in a timely manner. |
This comment was marked as duplicate.
This comment was marked as duplicate.
@kristof12345 friendly ping on support System.Text.Json, that would be an awesome addition to the library |
What's missing/not working in the code added in the PR? |
I made a quick look at the code and realized that the easiest way of solving this is to create two separate nugets that implements IJsonSerializer and then remove all code regarding serializing in the base nugget. JwtNet.Json.Newtonsoft: using System;
using JWT;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace Tests.ApiTests;
public class MyJsonSerializerNewtonsoft : IJsonSerializer
{
public string Serialize(object obj)
{
return JsonConvert
.SerializeObject(
obj,
new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = { new StringEnumConverter() }
});
}
public object Deserialize(Type type, string json)
{
return JsonConvert
.DeserializeObject(
json, type,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = { new StringEnumConverter() }
});
}
} JwtNet.Json.System.Text: using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using JWT;
namespace Tests.ApiTests;
public class MyJsonSystemText : IJsonSerializer
{
public string Serialize(object obj)
{
var jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
jsonOptions.Converters.Add(new JsonStringEnumConverter());
return JsonSerializer.Serialize(obj, jsonOptions);
}
public object Deserialize(Type type, string json)
{
var jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
jsonOptions.Converters.Add(new JsonStringEnumConverter());
return JsonSerializer.Deserialize(json, type, jsonOptions);
}
} Plus we need some way to configure the serlialization options. |
What didn't work? I got it compiling with this small changes to your branch. See this PR #401 However the asserts seems to be off as it complains about not matching types. When running the test: I get this error:
Note it's complaining it cannot compare "Jesus" with Jesus, when debugging it looks like this: It seems to be boxed to a JsonElement and not a proper string |
It seems Newtonsoft.Json does some guessing about what the type of the underlying object is and System.Text.Json does the safe way to not assume anything. So the fix is to add a custom deserializer like this: I'm going on vacation now and will be away until Tuesday so do a gently poke to me if you don't get it up and running. |
Hi!
Please update to .net 6 and replace Newtonsoft with System.Text.Json.
Thanks.
The text was updated successfully, but these errors were encountered: