make json writer class use built-in json parsing code instead of newtonsoft.json

This commit is contained in:
Chomp 2021-11-26 14:50:57 +00:00
parent fa59624172
commit b47769b290

View File

@ -1,9 +1,10 @@
using Common.Models.Output; using Common.Models.Output;
using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Common.Models.Input; using Common.Models.Input;
using System.Text.Json;
using System.Text.Unicode;
using System.Text.Encodings.Web;
namespace Common namespace Common
{ {
@ -12,6 +13,11 @@ namespace Common
private readonly string _workingPath; private readonly string _workingPath;
private readonly string _outputFolderName; private readonly string _outputFolderName;
private static JsonSerializerOptions ignoreNullOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true};
public JsonWriter(string workingPath, string outputFolderName) public JsonWriter(string workingPath, string outputFolderName)
{ {
_workingPath = workingPath; _workingPath = workingPath;
@ -23,6 +29,12 @@ namespace Common
var outputPath = $"{_workingPath}\\{_outputFolderName}"; var outputPath = $"{_workingPath}\\{_outputFolderName}";
DiskHelpers.CreateDirIfDoesntExist(outputPath); DiskHelpers.CreateDirIfDoesntExist(outputPath);
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
foreach (var bot in bots) foreach (var bot in bots)
{ {
if (bot.appearance.body.Count == 0) // only process files that have data in them, no body = no dumps if (bot.appearance.body.Count == 0) // only process files that have data in them, no body = no dumps
@ -30,7 +42,7 @@ namespace Common
LoggingHelpers.LogToConsole($"Unable to process bot type: {bot.botType}, skipping", ConsoleColor.DarkRed); LoggingHelpers.LogToConsole($"Unable to process bot type: {bot.botType}, skipping", ConsoleColor.DarkRed);
continue; continue;
} }
var output = JsonConvert.SerializeObject(bot, Formatting.Indented); var output = JsonSerializer.Serialize(bot, jsonOptions);
Console.WriteLine($"Writing json file {bot.botType} to {outputPath}"); Console.WriteLine($"Writing json file {bot.botType} to {outputPath}");
File.WriteAllText($"{outputPath}\\{bot.botType.ToString().ToLower()}.json", output); File.WriteAllText($"{outputPath}\\{bot.botType.ToString().ToLower()}.json", output);
Console.WriteLine($"file {bot.botType} written to {outputPath}"); Console.WriteLine($"file {bot.botType} written to {outputPath}");
@ -42,10 +54,7 @@ namespace Common
var outputPath = $"{_workingPath}\\{_outputFolderName}"; var outputPath = $"{_workingPath}\\{_outputFolderName}";
DiskHelpers.CreateDirIfDoesntExist(outputPath); DiskHelpers.CreateDirIfDoesntExist(outputPath);
var output = JsonConvert.SerializeObject(bots, Formatting.Indented, new JsonSerializerSettings var output = JsonSerializer.Serialize(bots, ignoreNullOptions);
{
NullValueHandling = NullValueHandling.Ignore
});
File.WriteAllText($"{outputPath}\\{fileName.ToLower()}.json", output); File.WriteAllText($"{outputPath}\\{fileName.ToLower()}.json", output);
} }
} }