Skip to content

Commit

Permalink
Newtonsoft config methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd committed Dec 5, 2023
1 parent c4dec2a commit cef9dfe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/Flurl.Http.Newtonsoft/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
using Flurl.Http.Configuration;
using Newtonsoft.Json;

namespace Flurl.Http.Newtonsoft
{
Expand Down Expand Up @@ -47,5 +49,23 @@ public static async Task<IList<dynamic>> ReceiveJsonList(this Task<IFlurlRespons
if (resp == null) return null;
return await resp.GetJsonListAsync().ConfigureAwait(false);
}

/// <summary>
/// Shortcut to use NewtonsoftJsonSerializer with this IFlurlClientBuilder.
/// </summary>
/// <param name="builder">This IFlurlClientBuilder.</param>
/// <param name="settings">Optional custom JsonSerializerSettings.</param>
/// <returns></returns>
public static IFlurlClientBuilder UseNewtonsoft(this IFlurlClientBuilder builder, JsonSerializerSettings settings = null) =>
builder.WithSettings(fs => fs.JsonSerializer = new NewtonsoftJsonSerializer(settings));

/// <summary>
/// Shortcut to use NewtonsoftJsonSerializer with all FlurlClients registered in this cache.
/// </summary>
/// <param name="cache">This IFlurlClientCache.</param>
/// <param name="settings">Optional custom JsonSerializerSettings.</param>
/// <returns></returns>
public static IFlurlClientCache UseNewtonsoft(this IFlurlClientCache cache, JsonSerializerSettings settings = null) =>
cache.WithDefaults(builder => builder.UseNewtonsoft(settings));
}
}
36 changes: 34 additions & 2 deletions test/Flurl.Test/Http/NewtonsoftTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using NUnit.Framework;
using System;
using NUnit.Framework;
using Flurl.Http;
using Flurl.Http.Newtonsoft;
using Flurl.Http.Testing;
using System.Threading.Tasks;
using Flurl.Http.Configuration;
using Newtonsoft.Json;

namespace Flurl.Test.Http
{
Expand Down Expand Up @@ -43,7 +46,7 @@ public async Task can_get_dynamic_list() {
}

[TestFixture, Parallelizable]
public class NewtonsofPostTests : PostTests
public class NewtonsoftPostTests : PostTests
{
protected override HttpTest CreateHttpTest() => base.CreateHttpTest()
.WithSettings(settings => settings.JsonSerializer = new NewtonsoftJsonSerializer());
Expand Down Expand Up @@ -73,4 +76,33 @@ public async Task can_receive_json_dynamic_list() {
Assert.AreEqual("Claire", data[1].name);
}
}

[TestFixture, Parallelizable]
public class NewtonsoftConfigTests
{
[Test]
public void can_register_with_builder() {
var cache = new FlurlClientCache();
var cli = cache.GetOrAdd("foo", null, builder => builder.UseNewtonsoft(new JsonSerializerSettings { DateFormatString = "1234" }));

Assert.IsInstanceOf<NewtonsoftJsonSerializer>(cli.Settings.JsonSerializer);

var obj = new { Date = DateTime.Now };
var json = cli.Settings.JsonSerializer.Serialize(obj);
Assert.AreEqual("{\"Date\":\"1234\"}", json);
}

[Test]
public void can_register_with_cache() {
var cache = new FlurlClientCache().UseNewtonsoft(new JsonSerializerSettings { DateFormatString = "1234" });
var cli = cache.GetOrAdd("foo");

Assert.IsInstanceOf<NewtonsoftJsonSerializer>(cli.Settings.JsonSerializer);

var obj = new { Date = DateTime.Now };
var json = cli.Settings.JsonSerializer.Serialize(obj);
Assert.AreEqual("{\"Date\":\"1234\"}", json);
}

}
}

0 comments on commit cef9dfe

Please sign in to comment.