From b47769b2905bad99cc8e2733e40295a90a6dc99e Mon Sep 17 00:00:00 2001 From: Chomp Date: Fri, 26 Nov 2021 14:50:57 +0000 Subject: [PATCH] make json writer class use built-in json parsing code instead of newtonsoft.json --- Common/JsonWriter.cs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Common/JsonWriter.cs b/Common/JsonWriter.cs index ce29c1d..0c30d46 100644 --- a/Common/JsonWriter.cs +++ b/Common/JsonWriter.cs @@ -1,9 +1,10 @@ using Common.Models.Output; -using Newtonsoft.Json; -using System; using System.Collections.Generic; using System.IO; using Common.Models.Input; +using System.Text.Json; +using System.Text.Unicode; +using System.Text.Encodings.Web; namespace Common { @@ -12,6 +13,11 @@ namespace Common private readonly string _workingPath; 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) { _workingPath = workingPath; @@ -23,6 +29,12 @@ namespace Common var outputPath = $"{_workingPath}\\{_outputFolderName}"; DiskHelpers.CreateDirIfDoesntExist(outputPath); + var jsonOptions = new JsonSerializerOptions() + { + WriteIndented = true, + Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) + }; + foreach (var bot in bots) { 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); continue; } - var output = JsonConvert.SerializeObject(bot, Formatting.Indented); + var output = JsonSerializer.Serialize(bot, jsonOptions); Console.WriteLine($"Writing json file {bot.botType} to {outputPath}"); File.WriteAllText($"{outputPath}\\{bot.botType.ToString().ToLower()}.json", output); Console.WriteLine($"file {bot.botType} written to {outputPath}"); @@ -42,10 +54,7 @@ namespace Common var outputPath = $"{_workingPath}\\{_outputFolderName}"; DiskHelpers.CreateDirIfDoesntExist(outputPath); - var output = JsonConvert.SerializeObject(bots, Formatting.Indented, new JsonSerializerSettings - { - NullValueHandling = NullValueHandling.Ignore - }); + var output = JsonSerializer.Serialize(bots, ignoreNullOptions); File.WriteAllText($"{outputPath}\\{fileName.ToLower()}.json", output); } }