Skip to content

Commit 0fe4281

Browse files
Subject: Refactored JSON operations and improved response writing
Body: Replaced `System.Text` namespace with `System.Text.Json` for JSON operations. Added two private static variables, `s_Options` and `JsonContentType`, for setting JSON writer options and response content type respectively. Converted `WriteResponse` method to asynchronous to allow non-blocking IO-bound operations. Modified `Utf8JsonWriter` to write directly to `context.Response.Body`, eliminating the need for `MemoryStream` to string conversion. Added `foreach` loops for iterating over `result.Entries` and `entry.Value.Data` collections, enabling writing each health check entry and its data to the JSON response. Simplified `JsonSerializer.Serialize` method by removing unnecessary line breaks. Included `writer.FlushAsync` method to ensure all buffered data is written to the underlying stream. Removed conversion of `MemoryStream` to string and writing it to the response as `Utf8JsonWriter` now writes directly to the response body.
1 parent 4be488e commit 0fe4281

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using System.Text.Json;
1+
using System.Text.Json;
32
using Microsoft.AspNetCore.Http;
43
using Microsoft.Extensions.Diagnostics.HealthChecks;
54

@@ -11,36 +10,36 @@ namespace XperienceCommunity.AspNetCore.HealthChecks
1110
/// <remarks>Provides a more detailed output than Health, Unhealthy, Degraded.</remarks>
1211
public static class HealthCheckResponseWriter
1312
{
13+
private static JsonWriterOptions s_Options = new JsonWriterOptions() { Indented = true };
14+
private const string JsonContentType = "application/json; charset=utf-8";
15+
1416
/// <summary>
1517
/// Create Health Check Response.
1618
/// </summary>
1719
/// <param name="context"></param>
1820
/// <param name="result"></param>
1921
/// <returns></returns>
20-
public static Task WriteResponse(HttpContext context, HealthReport result)
22+
public static async Task WriteResponse(HttpContext context, HealthReport result)
2123
{
22-
context.Response.ContentType = "application/json; charset=utf-8";
23-
24-
var options = new JsonWriterOptions {Indented = true};
24+
context.Response.ContentType = JsonContentType;
2525

26-
using var stream = new MemoryStream();
27-
using (var writer = new Utf8JsonWriter(stream, options))
26+
await using (var writer = new Utf8JsonWriter(context.Response.Body, s_Options))
2827
{
2928
writer.WriteStartObject();
3029
writer.WriteString("status", result.Status.ToString());
3130
writer.WriteStartObject("results");
31+
3232
foreach (var entry in result.Entries)
3333
{
3434
writer.WriteStartObject(entry.Key);
3535
writer.WriteString("status", entry.Value.Status.ToString());
3636
writer.WriteString("description", entry.Value.Description);
3737
writer.WriteStartObject("data");
38+
3839
foreach (var item in entry.Value.Data)
3940
{
4041
writer.WritePropertyName(item.Key);
41-
JsonSerializer.Serialize(
42-
writer, item.Value, item.Value?.GetType() ??
43-
typeof(object));
42+
JsonSerializer.Serialize(writer, item.Value, item.Value?.GetType() ?? typeof(object));
4443
}
4544

4645
writer.WriteEndObject();
@@ -49,11 +48,8 @@ public static Task WriteResponse(HttpContext context, HealthReport result)
4948

5049
writer.WriteEndObject();
5150
writer.WriteEndObject();
51+
await writer.FlushAsync();
5252
}
53-
54-
var json = Encoding.UTF8.GetString(stream.ToArray());
55-
56-
return context.Response.WriteAsync(json);
5753
}
5854
}
5955
}

0 commit comments

Comments
 (0)