Skip to content

Commit c6fff79

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

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Text.Json;
6-
using System.Text.Json.Serialization;
76
using System.Threading.Tasks;
87
using HealthChecks.UI.Client;
98
using Microsoft.AspNetCore.Http;
@@ -14,8 +13,6 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.HealthChecks
1413
{
1514
public class HealthCheckResponseWriter
1615
{
17-
private static readonly JsonSerializerOptions _options = CreateJsonOptions();
18-
1916
public static Task WriteResponseAsync(HttpContext httpContext, HealthReport report)
2017
{
2118
ArgumentNullException.ThrowIfNull(httpContext);
@@ -35,21 +32,7 @@ private static Task WriteMinimalResponseAsync(HttpContext httpContext, HealthRep
3532
{
3633
MinimalResponse body = new(report.Status);
3734
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;
35+
httpContext.Response.Body, body, JsonSerializerOptionsProvider.Options, httpContext.RequestAborted);
5336
}
5437

5538
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)