Json formatting for transcript: omit null objects

This commit is contained in:
Ben 2024-08-21 09:51:22 +02:00
parent 9bdebb963b
commit 5aa3edbda2
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
4 changed files with 35 additions and 8 deletions

View File

@ -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<EventBucketEntry>)
var entries = lines.Select(Json.Deserialize<EventBucketEntry>)
.Cast<EventBucketEntry>()
.OrderBy(e => e.Utc)
.ToArray();
File.Delete(bucketFile);
File.WriteAllLines(bucketFile, entries.Select(JsonConvert.SerializeObject));
File.WriteAllLines(bucketFile, entries.Select(e => Json.Serialize(e)));
}
}

View File

@ -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<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json)!;
}
}
}

View File

@ -132,7 +132,7 @@ namespace OverwatchTranscript
{
File.AppendAllLines(filePath, new[]
{
JsonConvert.SerializeObject(moment)
Json.Serialize(moment)
});
});
}

View File

@ -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");