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

Adding to JsonNetSerializer ctor accepting JsonSerializer #120

Merged
merged 6 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions JWT.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=HMACSHA/@EntryIndexedValue">HMACSHA</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RS/@EntryIndexedValue">RS</s:String>
<s:Boolean x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=69CA6CF3E63C5943A86891A91C864D7C/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=69CA6CF3E63C5943A86891A91C864D7C/RelativePath/@EntryValue">JWT.sln.DotSettings</s:String>
<s:Boolean x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File69CA6CF3E63C5943A86891A91C864D7C/@KeyIndexDefined">True</s:Boolean>
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File69CA6CF3E63C5943A86891A91C864D7C/RelativePriority/@EntryValue">1</s:Double>
</wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageProjectUrl>https://github.com/jwt-dotnet/jwt</PackageProjectUrl>
<Authors>John Sheehan, Michael Lehenbauer, Alexander Batishchev</Authors>
<PackageLicenseUrl>https://creativecommons.org/publicdomain/zero/1.0/</PackageLicenseUrl>
<Version>3.0.0-beta4</Version>
<Version>3.0.0</Version>
<PackageTags>jwt json</PackageTags>
</PropertyGroup>

Expand Down
33 changes: 23 additions & 10 deletions src/JWT/Serializers/JsonNetSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace JWT.Serializers
Expand All @@ -8,22 +9,34 @@ namespace JWT.Serializers
/// </summary>
public sealed class JsonNetSerializer : IJsonSerializer
{
private readonly JsonSerializer _serializer;

/// <summary>
/// Serialize the given object.
/// Creates a new instance of <see cref="JsonNetSerializer" />.
/// </summary>
/// <param name="obj">The object to serialize.</param>
/// <returns></returns>
public string Serialize(object obj)
/// <remarks>Uses <see cref="JsonSerializer.CreateDefault()" /> as internal serializer.</remarks>
public JsonNetSerializer()
: this(JsonSerializer.CreateDefault())
{
return JObject.FromObject(obj).ToString(Formatting.None);

}

/// <summary>
/// Deserialize the given string.
/// Creates a new instance of <see cref="JsonNetSerializer" />.
/// </summary>
/// <typeparam name="T">The type to deserialize the string to.</typeparam>
/// <param name="json">The JSON to be deserialized.</param>
/// <returns></returns>
/// <param name="serializer">Internal <see cref="JsonSerializer" /> to use for serialization.</param>
public JsonNetSerializer(JsonSerializer serializer)
{
_serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
}

/// <inheritdoc />
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL: inheritdoc 👍

public string Serialize(object obj)
{
return JObject.FromObject(obj, _serializer).ToString(Formatting.None);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would the Formatting.None override any formatting settings in the _serializer ?

Copy link
Member Author

Choose a reason for hiding this comment

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

IDK. Can you please try with your settings and see whether it works?

Copy link
Member Author

Choose a reason for hiding this comment

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

@PureKrome ping

Copy link
Contributor

Choose a reason for hiding this comment

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

Appols - RL > everything else right now. I'll give it a crack, tonight. watch this space :)

Copy link
Contributor

Choose a reason for hiding this comment

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

@abatishchev I feel like the formatting is ignored during the FromObject stage. And unfortunately, needs to be applied to the ToString() stage.

so ...

return JObject.FromObject(obj, _serializer).ToString(_serializer.Formatting);

BUT! because of me checking out that overload in the ToString() .. there's also a converters input param... which means this is also valid.

return JObject.FromObject(obj, _serializer).ToString(_serializer.Formatting, _serializer.Converters.ToArray());

This is me testing various permutations over the last 30-60 mins.
I've also ref'd these:

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh . that said ...

I couldn't find an online token decoder that RESPECTED the json formatting which was encoded into the JWT. All the online decoders I think formatted the json nicely for me (which makes all of this testing, hardish)

Copy link
Member Author

Choose a reason for hiding this comment

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

Check it out now

Copy link
Contributor

Choose a reason for hiding this comment

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

This LGTM 👍 I'm hoping the additions are actually worth it to other people 😊

Copy link
Member Author

Choose a reason for hiding this comment

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

One can only hope :-D

}

/// <inheritdoc />
public T Deserialize<T>(string json)
{
return JObject.Parse(json).ToObject<T>();
Expand Down
2 changes: 1 addition & 1 deletion tests/JWT.Tests.Common/JwtDecoderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using FluentAssertions;
using JWT.Algorithms;
using JWT.Serializers;
using Xunit;
using JWT.Tests.Common;
using Xunit;

namespace JWT.Tests
{
Expand Down
2 changes: 1 addition & 1 deletion tests/JWT.Tests.Common/JwtEncoderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using FluentAssertions;
using JWT.Algorithms;
using JWT.Serializers;
using Xunit;
using JWT.Tests.Common;
using Xunit;

namespace JWT.Tests
{
Expand Down