2021-09-18 22:36:59 +01:00
|
|
|
|
using Common.Models.Output;
|
2021-08-12 16:52:06 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2021-09-18 22:36:59 +01:00
|
|
|
|
using Common.Models.Input;
|
2021-11-26 14:50:57 +00:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Unicode;
|
|
|
|
|
using System.Text.Encodings.Web;
|
2021-08-12 16:52:06 +01:00
|
|
|
|
|
2021-09-18 22:36:59 +01:00
|
|
|
|
namespace Common
|
2021-08-12 16:52:06 +01:00
|
|
|
|
{
|
|
|
|
|
public class JsonWriter
|
|
|
|
|
{
|
|
|
|
|
private readonly string _workingPath;
|
|
|
|
|
private readonly string _outputFolderName;
|
|
|
|
|
|
2021-11-26 14:50:57 +00:00
|
|
|
|
private static JsonSerializerOptions ignoreNullOptions = new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
|
|
|
|
|
WriteIndented = true};
|
|
|
|
|
|
2021-08-12 16:52:06 +01:00
|
|
|
|
public JsonWriter(string workingPath, string outputFolderName)
|
|
|
|
|
{
|
|
|
|
|
_workingPath = workingPath;
|
|
|
|
|
_outputFolderName = outputFolderName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteJson(List<Bot> bots)
|
|
|
|
|
{
|
|
|
|
|
var outputPath = $"{_workingPath}\\{_outputFolderName}";
|
2021-08-24 12:08:30 +01:00
|
|
|
|
DiskHelpers.CreateDirIfDoesntExist(outputPath);
|
2021-08-12 16:52:06 +01:00
|
|
|
|
|
2021-11-26 14:50:57 +00:00
|
|
|
|
var jsonOptions = new JsonSerializerOptions()
|
|
|
|
|
{
|
|
|
|
|
WriteIndented = true,
|
|
|
|
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-12 16:52:06 +01:00
|
|
|
|
foreach (var bot in bots)
|
|
|
|
|
{
|
2021-08-17 18:33:55 +01:00
|
|
|
|
if (bot.appearance.body.Count == 0) // only process files that have data in them, no body = no dumps
|
|
|
|
|
{
|
2021-08-24 12:08:30 +01:00
|
|
|
|
LoggingHelpers.LogToConsole($"Unable to process bot type: {bot.botType}, skipping", ConsoleColor.DarkRed);
|
2021-08-17 18:33:55 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-11-26 14:50:57 +00:00
|
|
|
|
var output = JsonSerializer.Serialize(bot, jsonOptions);
|
2021-08-12 16:52:06 +01:00
|
|
|
|
Console.WriteLine($"Writing json file {bot.botType} to {outputPath}");
|
2021-09-01 17:40:03 +01:00
|
|
|
|
File.WriteAllText($"{outputPath}\\{bot.botType.ToString().ToLower()}.json", output);
|
2021-08-12 16:52:06 +01:00
|
|
|
|
Console.WriteLine($"file {bot.botType} written to {outputPath}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-18 22:36:59 +01:00
|
|
|
|
|
|
|
|
|
public void WriteJson(List<Datum> bots, string fileName)
|
|
|
|
|
{
|
|
|
|
|
var outputPath = $"{_workingPath}\\{_outputFolderName}";
|
|
|
|
|
DiskHelpers.CreateDirIfDoesntExist(outputPath);
|
|
|
|
|
|
2021-11-26 14:50:57 +00:00
|
|
|
|
var output = JsonSerializer.Serialize(bots, ignoreNullOptions);
|
2021-09-18 22:36:59 +01:00
|
|
|
|
File.WriteAllText($"{outputPath}\\{fileName.ToLower()}.json", output);
|
|
|
|
|
}
|
2021-08-12 16:52:06 +01:00
|
|
|
|
}
|
|
|
|
|
}
|