From 5aa3edbda22765106748ef19ddd19085f756384c Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 21 Aug 2024 09:51:22 +0200 Subject: [PATCH] Json formatting for transcript: omit null objects --- .../OverwatchTranscript/EventBucketWriter.cs | 8 +++--- Framework/OverwatchTranscript/Json.cs | 27 +++++++++++++++++++ .../MomentReferenceBuilder.cs | 2 +- .../OverwatchTranscript/TranscriptWriter.cs | 6 ++--- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 Framework/OverwatchTranscript/Json.cs diff --git a/Framework/OverwatchTranscript/EventBucketWriter.cs b/Framework/OverwatchTranscript/EventBucketWriter.cs index 179b7f4..00d3dbe 100644 --- a/Framework/OverwatchTranscript/EventBucketWriter.cs +++ b/Framework/OverwatchTranscript/EventBucketWriter.cs @@ -66,7 +66,7 @@ namespace OverwatchTranscript Event = new OverwatchEvent { Type = typeName, - Payload = JsonConvert.SerializeObject(payload) + Payload = Json.Serialize(payload) } }; @@ -84,7 +84,7 @@ namespace OverwatchTranscript using var writer = new StreamWriter(file); foreach (var entry in buffer) { - writer.WriteLine(JsonConvert.SerializeObject(entry)); + writer.WriteLine(Json.Serialize(entry)); } log.Debug($"Bucket wrote {buffer.Count} entries to file."); buffer.Clear(); @@ -94,13 +94,13 @@ namespace OverwatchTranscript private void SortFileByTimestamps() { var lines = File.ReadAllLines(bucketFile); - var entries = lines.Select(JsonConvert.DeserializeObject) + var entries = lines.Select(Json.Deserialize) .Cast() .OrderBy(e => e.Utc) .ToArray(); File.Delete(bucketFile); - File.WriteAllLines(bucketFile, entries.Select(JsonConvert.SerializeObject)); + File.WriteAllLines(bucketFile, entries.Select(e => Json.Serialize(e))); } } diff --git a/Framework/OverwatchTranscript/Json.cs b/Framework/OverwatchTranscript/Json.cs new file mode 100644 index 0000000..b97e3be --- /dev/null +++ b/Framework/OverwatchTranscript/Json.cs @@ -0,0 +1,27 @@ +using Newtonsoft.Json; +using System.Globalization; + +namespace OverwatchTranscript +{ + public static class Json + { + private static JsonSerializerSettings settings = new JsonSerializerSettings + { + Formatting = Formatting.None, + NullValueHandling = NullValueHandling.Ignore, + Culture = CultureInfo.InvariantCulture, + DateFormatHandling = DateFormatHandling.IsoDateFormat, + FloatFormatHandling = FloatFormatHandling.Symbol + }; + + public static string Serialize(object obj, Formatting formatting = Formatting.None) + { + return JsonConvert.SerializeObject(obj, formatting, settings); + } + + public static T Deserialize(string json) + { + return JsonConvert.DeserializeObject(json)!; + } + } +} diff --git a/Framework/OverwatchTranscript/MomentReferenceBuilder.cs b/Framework/OverwatchTranscript/MomentReferenceBuilder.cs index 77ba18e..616ebab 100644 --- a/Framework/OverwatchTranscript/MomentReferenceBuilder.cs +++ b/Framework/OverwatchTranscript/MomentReferenceBuilder.cs @@ -132,7 +132,7 @@ namespace OverwatchTranscript { File.AppendAllLines(filePath, new[] { - JsonConvert.SerializeObject(moment) + Json.Serialize(moment) }); }); } diff --git a/Framework/OverwatchTranscript/TranscriptWriter.cs b/Framework/OverwatchTranscript/TranscriptWriter.cs index d48f1a0..5c59b07 100644 --- a/Framework/OverwatchTranscript/TranscriptWriter.cs +++ b/Framework/OverwatchTranscript/TranscriptWriter.cs @@ -49,7 +49,7 @@ namespace OverwatchTranscript CheckClosed(); lock (_lock) { - header.Add(key, JsonConvert.SerializeObject(value)); + header.Add(key, Json.Serialize(value)); } } @@ -70,11 +70,11 @@ namespace OverwatchTranscript var momentReferences = builder.Build(bucketSet.FinalizeBuckets()); var model = CreateModel(momentReferences); - File.WriteAllText(transcriptFile, JsonConvert.SerializeObject(model, Formatting.Indented)); + File.WriteAllText(transcriptFile, Json.Serialize(model, Formatting.Indented)); ZipFile.CreateFromDirectory(workingDir, outputFilename); log.Debug($"Transcript written to {outputFilename}"); - log.Debug($"Common header: {JsonConvert.SerializeObject(model.Header.Common, Formatting.Indented)}"); + log.Debug($"Common header: {Json.Serialize(model.Header.Common, Formatting.Indented)}"); Directory.Delete(workingDir, true); log.Debug($"Workdir {workingDir} deleted");