diff --git a/ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs b/ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs index ff04c03..a97c6ca 100644 --- a/ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs +++ b/ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs @@ -1,6 +1,7 @@ using System.IO; using System.Text.Encodings.Web; using System.Text.Json; +using System.Text.Json.Serialization; using DTLib.Extensions; namespace ParadoxSaveParser.Lib.Tests; @@ -22,7 +23,8 @@ public class SearchExpressionTests { WriteIndented = false, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - MaxDepth = 1024 + MaxDepth = 1024, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, }; internal static string JsonToPdx(string json) diff --git a/ParadoxSaveParser.WebAPI/BackgroundTasks/SaveParsingOperation.cs b/ParadoxSaveParser.WebAPI/BackgroundTasks/SaveParsingOperation.cs index d19e452..ff873be 100644 --- a/ParadoxSaveParser.WebAPI/BackgroundTasks/SaveParsingOperation.cs +++ b/ParadoxSaveParser.WebAPI/BackgroundTasks/SaveParsingOperation.cs @@ -1,6 +1,7 @@ using System.IO.Compression; using System.Linq; using System.Text.Encodings.Web; +using System.Text.Json.Serialization; namespace ParadoxSaveParser.WebAPI.BackgroundTasks; @@ -15,9 +16,10 @@ public class SaveParsingOperation private static readonly JsonSerializerOptions _saveSerializerOptions = new() { - WriteIndented = true, + WriteIndented = false, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - MaxDepth = 1024 + MaxDepth = 1024, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, }; public SaveParsingOperation(long operationId, SaveFileMetadata meta, ISearchExpression searchQuery, diff --git a/ParadoxSaveParser.WebAPI/HttpHelpers/ReturnHelper.cs b/ParadoxSaveParser.WebAPI/HttpHelpers/ReturnHelper.cs index 369e52f..6c8de9f 100644 --- a/ParadoxSaveParser.WebAPI/HttpHelpers/ReturnHelper.cs +++ b/ParadoxSaveParser.WebAPI/HttpHelpers/ReturnHelper.cs @@ -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 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())); + } + } } \ No newline at end of file diff --git a/ParadoxSaveParser.WebAPI/SaveFileMetadata.cs b/ParadoxSaveParser.WebAPI/SaveFileMetadata.cs index 59e349a..6f24201 100644 --- a/ParadoxSaveParser.WebAPI/SaveFileMetadata.cs +++ b/ParadoxSaveParser.WebAPI/SaveFileMetadata.cs @@ -20,7 +20,11 @@ public enum Game public class SaveFileMetadata { - private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true }; + private static readonly JsonSerializerOptions _configSerializerOptions = new() + { + WriteIndented = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + }; public required string id { get; init; } [JsonConverter(typeof(JsonStringEnumConverter))] @@ -36,6 +40,6 @@ public class SaveFileMetadata public void SaveToFile() { using var metaFile = File.OpenWrite(PathHelper.GetMetaFilePath(id)); - JsonSerializer.Serialize(metaFile, this, _jsonOptions); + JsonSerializer.Serialize(metaFile, this, _configSerializerOptions); } } \ No newline at end of file