disabled null values serialization

This commit is contained in:
2025-04-06 15:55:58 +05:00
parent d70b605127
commit da27f84d68
4 changed files with 42 additions and 8 deletions
@@ -1,5 +1,7 @@
using System.Net;
using System.IO;
using System.Net;
using System.Text.Encodings.Web;
using System.Text.Json.Serialization;
using DTLib.Extensions;
namespace ParadoxSaveParser.WebAPI.HttpHelpers;
@@ -10,7 +12,8 @@ public class ReturnHelper
{
WriteIndented = false,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
MaxDepth = 1024
MaxDepth = 1024,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
private static async Task ResponseShort(HttpListenerContext ctx,
@@ -22,7 +25,7 @@ public class ReturnHelper
{
ctx.Response.StatusCode = (int)statusCode;
ctx.Response.ContentType = contentType;
logger.LogDebug($"response (length: {value.Length} type: {contentType})");
logger.LogDebug($"short response (length: {value.Length} type: {contentType})");
if (value.Length > 4000)
{
logger.LogWarn($"response (length: {value.Length} type: {contentType})\n"
@@ -67,4 +70,27 @@ public class ReturnHelper
{
return await ResponseJson(ctx, logger, ct, error, error.StatusCode);
}
public static async Task<HttpStatusCode> ResponseStream(HttpListenerContext ctx,
ContextLogger logger,
CancellationToken ct,
Stream valueStream,
HttpStatusCode statusCode = HttpStatusCode.OK,
string contentType = "application/octet-stream")
{
try
{
ctx.Response.StatusCode = (int)statusCode;
ctx.Response.ContentType = contentType;
logger.LogDebug($"stream response (type: {contentType})");
await valueStream.CopyToAsync(ctx.Response.OutputStream, ct);
return statusCode;
}
catch (Exception ex)
{
return await ResponseError(ctx, logger, ct,
new ErrorMessage(HttpStatusCode.InternalServerError,
ex.ToStringDemystified()));
}
}
}