using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Packing.lib
{
    public class JsonFileReader
    {
        public static async Task<T> ReadAsync<T>(string filePath)
        {
            FileStream stream = File.OpenRead(filePath);
            return await System.Text.Json.JsonSerializer.DeserializeAsync<T>(stream);
        }

        public static T Read<T>(string filePath)
        {
            string str = File.ReadAllText(filePath);
            return System.Text.Json.JsonSerializer.Deserialize<T>(str);
        }

        public static void Save<T>(T item, string filePath)
        {
            JsonSerializerSettings _options = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };

            var jsonString = JsonConvert.SerializeObject(item, _options);
            File.WriteAllText(filePath, jsonString);
        }
    }
}
