30 lines
936 B
C#
Raw Normal View History

2021-09-19 18:29:16 +01:00
using MarketPriceLookup.Helpers;
using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
namespace MarketPriceLookup.Common
{
public static class JsonWriter
{
public static void WriteJson<T>(T itemToSerialise, string outputFolderName, string workingPath, string fileName)
{
var outputPath = $"{workingPath}\\output\\{outputFolderName}";
DiskHelpers.CreateDirIfDoesntExist(outputPath);
Console.WriteLine($"Writing json file to {outputPath}");
var options = new JsonSerializerOptions
{
WriteIndented = true,
IgnoreNullValues = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
var json = JsonSerializer.Serialize(itemToSerialise, options);
File.WriteAllText($"{outputPath}\\{fileName}.json", json);
}
}
}