Skip to content

Commit 5ab9426

Browse files
committed
Extract JsonSerializerOptions to reusable static
1 parent ea8b88a commit 5ab9426

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/WebJobs.Script.WebHost/Diagnostics/HealthChecks/HealthCheckResponseWriter.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.HealthChecks
1414
{
1515
public class HealthCheckResponseWriter
1616
{
17-
private static readonly JsonSerializerOptions _options = CreateJsonOptions();
18-
1917
public static Task WriteResponseAsync(HttpContext httpContext, HealthReport report)
2018
{
2119
ArgumentNullException.ThrowIfNull(httpContext);
@@ -35,21 +33,7 @@ private static Task WriteMinimalResponseAsync(HttpContext httpContext, HealthRep
3533
{
3634
MinimalResponse body = new(report.Status);
3735
return JsonSerializer.SerializeAsync(
38-
httpContext.Response.Body, body, _options, httpContext.RequestAborted);
39-
}
40-
41-
private static JsonSerializerOptions CreateJsonOptions()
42-
{
43-
var options = new JsonSerializerOptions
44-
{
45-
AllowTrailingCommas = true,
46-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
47-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
48-
};
49-
50-
options.Converters.Add(new JsonStringEnumConverter());
51-
52-
return options;
36+
httpContext.Response.Body, body, JsonSerializerOptionsProvider.Options, httpContext.RequestAborted);
5337
}
5438

5539
internal readonly struct MinimalResponse(HealthStatus status)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace Microsoft.Azure.WebJobs.Script
8+
{
9+
/// <summary>
10+
/// Provides constants related to JSON serialization options used.
11+
/// </summary>
12+
public static class JsonSerializerOptionsProvider
13+
{
14+
/// <summary>
15+
/// Gets the default JSON serializer options used across the functions host.
16+
/// </summary>
17+
public static readonly JsonSerializerOptions Options = CreateJsonOptions();
18+
19+
private static JsonSerializerOptions CreateJsonOptions()
20+
{
21+
var options = new JsonSerializerOptions
22+
{
23+
AllowTrailingCommas = true,
24+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
25+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
26+
};
27+
28+
options.Converters.Add(new JsonStringEnumConverter());
29+
30+
return options;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)