Skip to content

Commit 75609cd

Browse files
committed
Move statics outside of WebhookContent
1 parent 5914279 commit 75609cd

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/StandardWebhooks/WebhookContent.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
using System.Diagnostics.CodeAnalysis;
88
using System.Net.Http.Headers;
9-
using System.Text;
109
using System.Text.Json;
1110
using System.Text.Json.Serialization;
1211

@@ -18,10 +17,6 @@ namespace StandardWebhooks;
1817
/// <typeparam name="T">The type of the content.</typeparam>
1918
public class WebhookContent<T> : ByteArrayContent
2019
{
21-
private static readonly Encoding Utf8Encoding = new UTF8Encoding(false, true);
22-
private static readonly JsonSerializerOptions DefaultJsonSerializerOptions =
23-
new JsonSerializerOptions(JsonSerializerDefaults.Web) { WriteIndented = false };
24-
2520
// Maintain copy of the content so we can return it as a string.
2621
private readonly byte[] _content;
2722

@@ -30,7 +25,7 @@ private WebhookContent(byte[] content)
3025
{
3126
_content = content;
3227

33-
Headers.ContentType = new MediaTypeHeaderValue("application/json") { CharSet = Utf8Encoding.WebName };
28+
Headers.ContentType = new MediaTypeHeaderValue("application/json") { CharSet = WebhookContentDefaults.Utf8Encoding.WebName };
3429
}
3530

3631
/// <summary>Creates a new instance of the <see cref="WebhookContent{T}"/> class.</summary>
@@ -42,7 +37,7 @@ private WebhookContent(byte[] content)
4237
[RequiresUnreferencedCode("This code path does not support NativeAOT. Use the JsonSerializationContext overload for NativeAOT Scenarios.")]
4338
public static WebhookContent<T> Create(T content, JsonSerializerOptions? jsonOptions = null)
4439
{
45-
var utf8bytes = JsonSerializer.SerializeToUtf8Bytes(content, jsonOptions ?? DefaultJsonSerializerOptions);
40+
var utf8bytes = JsonSerializer.SerializeToUtf8Bytes(content, jsonOptions ?? WebhookContentDefaults.JsonSerializerOptions);
4641

4742
return new WebhookContent<T>(utf8bytes);
4843
}
@@ -62,5 +57,5 @@ public static WebhookContent<T> Create(T content, JsonSerializerContext context)
6257
/// Gets the content as a string.
6358
/// </summary>
6459
/// <returns>String representation of the content.</returns>
65-
public override string ToString() => Utf8Encoding.GetString(_content);
66-
}
60+
public override string ToString() => WebhookContentDefaults.Utf8Encoding.GetString(_content);
61+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2024, Codefactors Ltd.
2+
//
3+
// Codefactors Ltd licenses this file to you under the following license(s):
4+
//
5+
// * The MIT License, see https://opensource.org/license/mit/
6+
7+
using System.Text;
8+
using System.Text.Json;
9+
10+
namespace StandardWebhooks
11+
{
12+
/// <summary>
13+
/// Internal defaults for <see cref="WebhookContent{T}"/>
14+
/// </summary>
15+
internal static class WebhookContentDefaults
16+
{
17+
/// <summary>
18+
/// Default Uft8Encoding instance.
19+
/// </summary>
20+
public static readonly Encoding Utf8Encoding = new UTF8Encoding(false, true);
21+
22+
/// <summary>
23+
/// Default JsonSerializerOptions instance.
24+
/// </summary>
25+
public static readonly JsonSerializerOptions JsonSerializerOptions =
26+
new JsonSerializerOptions(JsonSerializerDefaults.Web) { WriteIndented = false };
27+
}
28+
}

0 commit comments

Comments
 (0)