aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-18 20:07:29 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:53 +0800
commitf73342cb6b0592d3a6310f9a12fd40b4bf218e5c (patch)
tree705eac7dd02e4335816a4b048b1b14d08cea24a4 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs
parent7407875fb75bcd90f6f7ef54573483fe2f3cfb84 (diff)
downloadcrupest-f73342cb6b0592d3a6310f9a12fd40b4bf218e5c.tar.gz
crupest-f73342cb6b0592d3a6310f9a12fd40b4bf218e5c.tar.bz2
crupest-f73342cb6b0592d3a6310f9a12fd40b4bf218e5c.zip
Develop secret api. v42
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs62
1 files changed, 0 insertions, 62 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs
deleted file mode 100644
index 61bcc61..0000000
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs
+++ /dev/null
@@ -1,62 +0,0 @@
-using System.Text.Json;
-using Microsoft.Extensions.Options;
-using Microsoft.Net.Http.Headers;
-
-namespace CrupestApi.Commons;
-
-public static class CrupestApiJsonExtensions
-{
- public static IServiceCollection AddJsonOptions(this IServiceCollection services)
- {
- services.AddOptions<JsonSerializerOptions>();
- services.Configure<JsonSerializerOptions>(config =>
- {
- config.AllowTrailingCommas = true;
- config.PropertyNameCaseInsensitive = true;
- config.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
- });
-
- return services;
- }
-
- public static async Task<JsonDocument> ReadJsonAsync(this HttpRequest request)
- {
- using var stream = request.Body;
- return await JsonDocument.ParseAsync(stream);
- }
-
- public static async Task WriteJsonAsync<T>(this HttpResponse response, T bodyObject, int statusCode = 200, HttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default)
- {
- var jsonOptions = response.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<JsonSerializerOptions>>();
- byte[] json = JsonSerializer.SerializeToUtf8Bytes<T>(bodyObject, jsonOptions.Value);
-
- var byteCount = json.Length;
-
- response.StatusCode = statusCode;
- response.Headers.ContentType = "application/json; charset=utf-8";
- response.Headers.ContentLength = byteCount;
-
- if (beforeWriteBody is not null)
- {
- beforeWriteBody(response);
- }
-
- await response.Body.WriteAsync(json, cancellationToken);
- }
-
- public static async Task WriteMessageAsync(this HttpResponse response, string message, int statusCode = 400, HttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default)
- {
- await response.WriteJsonAsync(new ErrorBody(message), statusCode: statusCode, beforeWriteBody, cancellationToken);
- }
-
- public static Task ResponseJsonAsync<T>(this HttpContext context, T bodyObject, int statusCode = 200, HttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default)
- {
- return context.Response.WriteJsonAsync<T>(bodyObject, statusCode, beforeWriteBody, cancellationToken);
- }
-
- public static Task ResponseMessageAsync(this HttpContext context, string message, int statusCode = 400, HttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default)
- {
- return context.Response.WriteMessageAsync(message, statusCode, beforeWriteBody, cancellationToken);
- }
-
-}