Skip to content

Commit 5979be5

Browse files
pass json serializer options to http extension methods
1 parent 455ded2 commit 5979be5

5 files changed

Lines changed: 79 additions & 34 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
## Installation
1515
Download from [NuGet](https://www.nuget.org/packages/Tizzani.QueryStringSerializer).
1616
```
17-
dotnet add package Tizzani.QueryStringSerializer --version 9.0.0
17+
dotnet add package Tizzani.QueryStringSerializer --version 9.1.1
1818
```
1919

2020
## Example Usage

src/Tizzani.QueryStringSerializer.Blazor/Tizzani.QueryStringSerializer.Blazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Copyright>2025 Erin McLaughlin</Copyright>
1010
<RepositoryUrl>https://github.com/erinnmclaughlin/Tizzani.QueryStringSerializer</RepositoryUrl>
1111
<PackageLicenseExpression>MIT</PackageLicenseExpression>
12-
<Version>9.1.0</Version>
12+
<Version>9.2.0-preview1</Version>
1313
<PackageIcon>QssBlazorLogo.png</PackageIcon>
1414
</PropertyGroup>
1515

src/Tizzani.QueryStringSerializer/HttpClientExtensions.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
using System.Diagnostics.CodeAnalysis;
2+
using System.Text.Json;
23
using Tizzani.QueryStringSerializer;
34

45
// ReSharper disable once CheckNamespace
56
namespace System.Net.Http.Json;
67

8+
/// <summary>
9+
/// Extension methods for <see cref="HttpClient"/> to support query string serialization.
10+
/// </summary>
711
public static class HttpClientExtensions
812
{
913
/// <summary>
@@ -14,13 +18,35 @@ public static class HttpClientExtensions
1418
/// <param name="requestQuery">The object that should be serialized to the request query string.</param>
1519
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
1620
/// <returns>The response message.</returns>
21+
public static Task<HttpResponseMessage> GetWithQueryAsync(
22+
this HttpClient client,
23+
[StringSyntax("Uri")] string requestUri,
24+
object? requestQuery,
25+
CancellationToken cancellationToken = default) =>
26+
client.GetWithQueryAsync(
27+
requestUri,
28+
requestQuery,
29+
QueryStringSerializer.DefaultSerializerOptions,
30+
cancellationToken
31+
);
32+
33+
/// <summary>
34+
/// Sends a GET request to the specified URL.
35+
/// </summary>
36+
/// <param name="client">The client used to send the request.</param>
37+
/// <param name="requestUri">The URI the request is sent to.</param>
38+
/// <param name="requestQuery">The object that should be serialized to the request query string.</param>
39+
/// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
40+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
41+
/// <returns>The response message.</returns>
1742
public static async Task<HttpResponseMessage> GetWithQueryAsync(
1843
this HttpClient client,
1944
[StringSyntax("Uri")] string requestUri,
2045
object? requestQuery,
46+
JsonSerializerOptions jsonSerializerOptions,
2147
CancellationToken cancellationToken = default)
2248
{
23-
var uri = QueryStringSerializer.Serialize(requestUri, requestQuery);
49+
var uri = QueryStringSerializer.Serialize(requestUri, requestQuery, jsonSerializerOptions);
2450
return await client.GetAsync(uri, cancellationToken);
2551
}
2652

@@ -33,13 +59,36 @@ public static async Task<HttpResponseMessage> GetWithQueryAsync(
3359
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
3460
/// <typeparam name="T">The type to deserialize to.</typeparam>
3561
/// <returns>The deserialized response.</returns>
62+
public static Task<T?> GetFromJsonWithQueryAsync<T>(
63+
this HttpClient client,
64+
[StringSyntax("Uri")] string requestUri,
65+
object? requestQuery,
66+
CancellationToken cancellationToken = default) =>
67+
client.GetFromJsonWithQueryAsync<T>(
68+
requestUri,
69+
requestQuery,
70+
QueryStringSerializer.DefaultSerializerOptions,
71+
cancellationToken
72+
);
73+
74+
/// <summary>
75+
/// Sends a GET request to the specified URL and deserializes the response into an object of type <typeparamref name="T"/>.
76+
/// </summary>
77+
/// <param name="client">The client used to send the request.</param>
78+
/// <param name="requestUri">The URI the request is sent to.</param>
79+
/// <param name="requestQuery">The object that should be serialized to the request query string.</param>
80+
/// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
81+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
82+
/// <typeparam name="T">The type to deserialize to.</typeparam>
83+
/// <returns>The deserialized response.</returns>
3684
public static async Task<T?> GetFromJsonWithQueryAsync<T>(
3785
this HttpClient client,
3886
[StringSyntax("Uri")] string requestUri,
3987
object? requestQuery,
88+
JsonSerializerOptions jsonSerializerOptions,
4089
CancellationToken cancellationToken = default)
4190
{
42-
var uri = QueryStringSerializer.Serialize(requestUri, requestQuery);
43-
return await client.GetFromJsonAsync<T>(uri, cancellationToken);
91+
var uri = QueryStringSerializer.Serialize(requestUri, requestQuery, jsonSerializerOptions);
92+
return await client.GetFromJsonAsync<T>(uri, jsonSerializerOptions, cancellationToken);
4493
}
4594
}

src/Tizzani.QueryStringSerializer/QueryStringSerializer.cs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,43 @@ namespace Tizzani.QueryStringSerializer;
1111

1212
public static class QueryStringSerializer
1313
{
14+
internal static readonly JsonSerializerOptions DefaultSerializerOptions;
15+
16+
static QueryStringSerializer()
17+
{
18+
DefaultSerializerOptions = new JsonSerializerOptions();
19+
DefaultSerializerOptions.Converters.Add(new JsonStringEnumConverter());
20+
}
21+
1422
/// <summary>
1523
/// Serializes an object into a query string format.
1624
/// </summary>
1725
/// <param name="uri">The destination URI.</param>
1826
/// <param name="obj">The object to serialize into a query string.</param>
19-
/// <param name="options">The options to use for serialization.</param>
20-
/// <typeparam name="T">The type being serialized.</typeparam>
27+
/// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
2128
/// <returns>The resulting uri and query string.</returns>
22-
public static string Serialize<T>(
29+
public static string Serialize(
2330
[StringSyntax("Uri")] string uri,
24-
T? obj,
25-
JsonSerializerOptions? options = null)
31+
object? obj,
32+
JsonSerializerOptions? jsonSerializerOptions = null)
2633
{
27-
return obj is null ? uri : $"{uri}?{Serialize(obj, options)}";
34+
return obj is null ? uri : $"{uri}?{Serialize(obj, jsonSerializerOptions)}";
2835
}
2936

3037
/// <summary>
3138
/// Serializes an object into a query string format.
3239
/// </summary>
3340
/// <param name="obj">The object to serialize into a query string.</param>
34-
/// <param name="options">The options to use for serialization.</param>
35-
/// <typeparam name="T">The type being serialized.</typeparam>
41+
/// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
3642
/// <returns>The resulting uri and query string.</returns>
37-
public static string Serialize<T>(
38-
T? obj,
39-
JsonSerializerOptions? options = null)
43+
public static string Serialize(
44+
object? obj,
45+
JsonSerializerOptions? jsonSerializerOptions = null)
4046
{
4147
if (obj is null)
4248
return "";
43-
44-
if (options == null)
45-
{
46-
options = new JsonSerializerOptions();
47-
options.Converters.Add(new JsonStringEnumConverter());
48-
}
4949

50-
var json = JsonSerializer.Serialize(obj, options);
50+
var json = JsonSerializer.Serialize(obj, jsonSerializerOptions ?? DefaultSerializerOptions);
5151

5252
var jObject = JsonNode.Parse(json);
5353
return ParseToken(jObject);
@@ -57,24 +57,20 @@ public static string Serialize<T>(
5757
/// Deserializes a query string into an object of the specified type.
5858
/// </summary>
5959
/// <param name="uri">The uri that contains the query string.</param>
60-
/// <param name="options">The options to use for deserialization.</param>
60+
/// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
6161
/// <typeparam name="T">The type to deserialize to.</typeparam>
6262
/// <returns></returns>
6363
public static T? Deserialize<T>(
6464
[StringSyntax("Uri")] string uri,
65-
JsonSerializerOptions? options = null)
65+
JsonSerializerOptions? jsonSerializerOptions = null)
6666
{
67-
if (options == null)
68-
{
69-
options = new JsonSerializerOptions();
70-
options.Converters.Add(new JsonStringEnumConverter());
71-
}
67+
jsonSerializerOptions ??= DefaultSerializerOptions;
7268

7369
var qs = uri.Contains('?') ? uri.Split('?')[1] : uri;
7470
var qParams = QueryHelpers.ParseQuery(qs);
75-
var dict = GetObjectDictionary(typeof(T), qParams, options);
76-
var json = JsonSerializer.Serialize(dict, options);
77-
return JsonSerializer.Deserialize<T>(json, options);
71+
var dict = GetObjectDictionary(typeof(T), qParams, jsonSerializerOptions);
72+
var json = JsonSerializer.Serialize(dict, jsonSerializerOptions);
73+
return JsonSerializer.Deserialize<T>(json, jsonSerializerOptions);
7874
}
7975

8076
private static Dictionary<string, object?> GetObjectDictionary(Type objectType, Dictionary<string, StringValues> qParams, JsonSerializerOptions jsonSerializerOptions)

src/Tizzani.QueryStringSerializer/Tizzani.QueryStringSerializer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Copyright>2025 Erin McLaughlin</Copyright>
1212
<Description>Tizzani.QueryStringHelpers provides methods for serializing &amp; deserializing objects as URL-encoded query strings.</Description>
1313
<PackageLicenseExpression>MIT</PackageLicenseExpression>
14-
<Version>9.1.0</Version>
14+
<Version>9.2.0-preview1</Version>
1515
<Title>$(AssemblyName)</Title>
1616
<PackageIcon>QssLogo.png</PackageIcon>
1717
<PackageProjectUrl>https://github.com/erinnmclaughlin/Tizzani.QueryStringSerializer</PackageProjectUrl>

0 commit comments

Comments
 (0)