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 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
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
36 changes: 25 additions & 11 deletions src/JWT/Serializers/JsonNetSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Newtonsoft.Json;
using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace JWT.Serializers
Expand All @@ -8,25 +10,37 @@ 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(_serializer.Formatting, _serializer.Converters.ToArray());
}

/// <inheritdoc />
public T Deserialize<T>(string json)
{
return JObject.Parse(json).ToObject<T>();
return JObject.Parse(json).ToObject<T>(_serializer);
}
}
}
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