Skip to content

Commit

Permalink
Adding to JsonNetSerializer ctor accepting JsonSerializer (#120)
Browse files Browse the repository at this point in the history
* Adding to JsonNetSerializer ctor accepting JsonSerializer
* Removing the beta prefix from nuget version
* Replacing xml docs on methods with <inheritdoc />
* Soring usings in tests
* Updating R# settings files
* Using _serializer.Formatting, _serializer.Converters in Serialize(), Deserialize()
  • Loading branch information
abatishchev authored Jul 5, 2017
1 parent d51a8a3 commit d639be2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
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 />
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

0 comments on commit d639be2

Please sign in to comment.