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

Replace Newtonsoft.Json with System.Text.Json #283

Closed
wants to merge 6 commits into from

Conversation

luposky
Copy link

@luposky luposky commented May 15, 2020

Now (only in netstandard20) you can use SystemTextJsonSerializer as a serializer.
Some limitations apply, resolves #218.

@abatishchev abatishchev changed the title fix #218 Replace Newtonsoft.Json with System.Text.Json May 16, 2020
@abatishchev abatishchev added this to the 8.0 milestone May 16, 2020
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
Copy link
Member

Choose a reason for hiding this comment

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

Should we reference either one or another?

Copy link
Member

Choose a reason for hiding this comment

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

And the another, already existing serializer must be included only if the target framework is lower than netstd2

Copy link
Author

Choose a reason for hiding this comment

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

I don't see any problem in referencing Newtonsoft.Json in netstandard20 ...
I simply like the option to use System.Text.Json ...
maybe in the future with net50 things can be different and we can drop support for Newtonsoft.Json

@@ -8,24 +11,45 @@ namespace JWT.Builder
public class JwtHeader
{
[JsonProperty("typ")]
#if SYSTEMTEXTJSON
Copy link
Member

@abatishchev abatishchev May 16, 2020

Choose a reason for hiding this comment

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

Should it be one or another (conditionally)? I would define attribute alias in the top between #if #else #endif

Copy link
Author

Choose a reason for hiding this comment

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

I really don't know .. freedom of choiche is a value ... using netstandard20 I can choose beetween the two types of serializers (like in the asp.net core framework) so why forcing to use System.Text.Json if even microsoft don't force us to do so?

Copy link
Member

Choose a reason for hiding this comment

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

Otherwise "replacing" won't be replacing but rather adding one more dependency, what users of Json.Net won't need hence would rightfully question.
Ideally either serializer would need to go to its own satellite package so users would be free to choose between one or another or maybe something third.
My point is that having both attributes and serializers basically means referencing them all and piling their respective attributes on each of this property.
I can easily imagine Unity folks or someone on .NET 3.5 who's unable to use either and who use DataContractJsonSerializer instead coming here and asking to add a third one.
So instead I would switch from a 3rd party to the 1st party serializer (and hope most of the users would be fine with that).

Copy link
Author

@luposky luposky May 16, 2020

Choose a reason for hiding this comment

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

yes, I understand ... I leave this choice to you ... I will push another commit with some improvment ...

var value = (JsonElement)odata[key];
switch (value.ValueKind)
{
case JsonValueKind.Undefined:
Copy link
Member

Choose a reason for hiding this comment

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

Please group all cases that do nothing into a single block

case JsonValueKind.Array:
break;
case JsonValueKind.String:
ndata.Add(key, value.GetString());
Copy link
Member

Choose a reason for hiding this comment

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

And same here: multiple cases - one block of code

src/JWT/Serializers/SystemTextJsonSerializer.cs Outdated Show resolved Hide resolved
// Implement using favorite JSON serializer
var data = JsonSerializer.Deserialize<T>(json);

if (data is Dictionary<string, object> odata)
Copy link
Member

Choose a reason for hiding this comment

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

Invert the if?

src/JWT/Serializers/SystemTextJsonSerializer.cs Outdated Show resolved Hide resolved
src/JWT/JWT.csproj Show resolved Hide resolved
Copy link
Member

@abatishchev abatishchev left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, coming back whenever I have time

}
}

return ndata is T obj ? obj : data;
Copy link
Member

Choose a reason for hiding this comment

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

Can you please explain this line?

Copy link
Author

Choose a reason for hiding this comment

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

You cannot "return ndata" because ndata is an object of type Dictionary<string, object> not of type T. This expression cast ndata to the type T if possible. If it is not possible, default to return the object previuosly deserialized as is. Compiler know nothing about the relationship between T and Dictionary<string, object>, so this trick (or similar) is required.

@abatishchev
Copy link
Member

abatishchev commented May 19, 2020

What would you say if I propose to move the whole thing to a Jwt.Serializers.SystemTextJson package? Which would have its own version of the header model, decorated accordingly. You won't be able to use one of the DecodeHeader() methods but will be able the another.

Ideally I'd Invision to move all Json.Net-dependent classes to a separate package as well. This idea was outline long time ago in #76.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

Replace Newtonsoft.Json with System.Text.Json
2 participants