Json formatting for transcript: omit null objects
This commit is contained in:
parent
9bdebb963b
commit
5aa3edbda2
@ -66,7 +66,7 @@ namespace OverwatchTranscript
|
|||||||
Event = new OverwatchEvent
|
Event = new OverwatchEvent
|
||||||
{
|
{
|
||||||
Type = typeName,
|
Type = typeName,
|
||||||
Payload = JsonConvert.SerializeObject(payload)
|
Payload = Json.Serialize(payload)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ namespace OverwatchTranscript
|
|||||||
using var writer = new StreamWriter(file);
|
using var writer = new StreamWriter(file);
|
||||||
foreach (var entry in buffer)
|
foreach (var entry in buffer)
|
||||||
{
|
{
|
||||||
writer.WriteLine(JsonConvert.SerializeObject(entry));
|
writer.WriteLine(Json.Serialize(entry));
|
||||||
}
|
}
|
||||||
log.Debug($"Bucket wrote {buffer.Count} entries to file.");
|
log.Debug($"Bucket wrote {buffer.Count} entries to file.");
|
||||||
buffer.Clear();
|
buffer.Clear();
|
||||||
@ -94,13 +94,13 @@ namespace OverwatchTranscript
|
|||||||
private void SortFileByTimestamps()
|
private void SortFileByTimestamps()
|
||||||
{
|
{
|
||||||
var lines = File.ReadAllLines(bucketFile);
|
var lines = File.ReadAllLines(bucketFile);
|
||||||
var entries = lines.Select(JsonConvert.DeserializeObject<EventBucketEntry>)
|
var entries = lines.Select(Json.Deserialize<EventBucketEntry>)
|
||||||
.Cast<EventBucketEntry>()
|
.Cast<EventBucketEntry>()
|
||||||
.OrderBy(e => e.Utc)
|
.OrderBy(e => e.Utc)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
File.Delete(bucketFile);
|
File.Delete(bucketFile);
|
||||||
File.WriteAllLines(bucketFile, entries.Select(JsonConvert.SerializeObject));
|
File.WriteAllLines(bucketFile, entries.Select(e => Json.Serialize(e)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
27
Framework/OverwatchTranscript/Json.cs
Normal file
27
Framework/OverwatchTranscript/Json.cs
Normal 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)!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -132,7 +132,7 @@ namespace OverwatchTranscript
|
|||||||
{
|
{
|
||||||
File.AppendAllLines(filePath, new[]
|
File.AppendAllLines(filePath, new[]
|
||||||
{
|
{
|
||||||
JsonConvert.SerializeObject(moment)
|
Json.Serialize(moment)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ namespace OverwatchTranscript
|
|||||||
CheckClosed();
|
CheckClosed();
|
||||||
lock (_lock)
|
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 momentReferences = builder.Build(bucketSet.FinalizeBuckets());
|
||||||
var model = CreateModel(momentReferences);
|
var model = CreateModel(momentReferences);
|
||||||
|
|
||||||
File.WriteAllText(transcriptFile, JsonConvert.SerializeObject(model, Formatting.Indented));
|
File.WriteAllText(transcriptFile, Json.Serialize(model, Formatting.Indented));
|
||||||
|
|
||||||
ZipFile.CreateFromDirectory(workingDir, outputFilename);
|
ZipFile.CreateFromDirectory(workingDir, outputFilename);
|
||||||
log.Debug($"Transcript written to {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);
|
Directory.Delete(workingDir, true);
|
||||||
log.Debug($"Workdir {workingDir} deleted");
|
log.Debug($"Workdir {workingDir} deleted");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user