Skip to content

Commit

Permalink
Add Newtonsoft serializer to test project
Browse files Browse the repository at this point in the history
add tests later, maybe
  • Loading branch information
tmenier committed Jun 5, 2022
1 parent b081ce3 commit 869e9c6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Test/Flurl.Test/Flurl.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net48;net461</TargetFrameworks>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -9,6 +10,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
</ItemGroup>
Expand Down
45 changes: 45 additions & 0 deletions Test/Flurl.Test/Http/NewtonsoftJsonSerializer .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.IO;
using Newtonsoft.Json;

namespace Flurl.Http.Configuration
{
/// <summary>
/// ISerializer implementation based on Newtonsoft.Json.
/// Default serializer used in calls to GetJsonAsync, PostJsonAsync, etc.
/// </summary>
public class NewtonsoftJsonSerializer : ISerializer
{
private readonly JsonSerializerSettings _settings;

/// <summary>
/// Initializes a new instance of the <see cref="NewtonsoftJsonSerializer"/> class.
/// </summary>
/// <param name="settings">Settings to control (de)serialization behavior.</param>
public NewtonsoftJsonSerializer(JsonSerializerSettings settings = null) {
_settings = settings;
}

/// <summary>
/// Serializes the specified object to a JSON string.
/// </summary>
/// <param name="obj">The object to serialize.</param>
public string Serialize(object obj) => JsonConvert.SerializeObject(obj, _settings);

/// <summary>
/// Deserializes the specified JSON string to an object of type T.
/// </summary>
/// <param name="s">The JSON string to deserialize.</param>
public T Deserialize<T>(string s) => JsonConvert.DeserializeObject<T>(s, _settings);

/// <summary>
/// Deserializes the specified stream to an object of type T.
/// </summary>
/// <param name="stream">The stream to deserialize.</param>
public T Deserialize<T>(Stream stream) {
// https://www.newtonsoft.com/json/help/html/Performance.htm#MemoryUsage
using var sr = new StreamReader(stream);
using var jr = new JsonTextReader(sr);
return JsonSerializer.CreateDefault(_settings).Deserialize<T>(jr);
}
}
}
1 change: 0 additions & 1 deletion src/Flurl.Http/Configuration/DefaultJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Security.Cryptography;
using System.Text.Json;

namespace Flurl.Http.Configuration
Expand Down

0 comments on commit 869e9c6

Please sign in to comment.