using Newtonsoft.Json;
using Newtonsoft.Json.Bson;

namespace adas_core.Domain.Utils;

/// <summary>
/// Provides extension methods for JSON-related operations.
/// </summary>
public static class JsonExtensions
{
    /// <summary>
    /// Reads JSON content from the specified input file and writes it as BSON to the output file.
    /// By default, uses <see cref="FileMode.CreateNew"/>, which requires the output file to not already exist.
    /// </summary>
    /// <param name="inputPath">The path to the source JSON file to read from.</param>
    /// <param name="outputPath">The path to the destination BSON file to write to.</param>
    /// <param name="fileMode">The file mode used to open the output file. Defaults to <see cref="FileMode.CreateNew"/>.</param>
    public static void CopyToBson(string inputPath, string outputPath, FileMode fileMode = FileMode.CreateNew)
        {
            using var textReader = File.OpenText(inputPath);
            using var jsonReader = new JsonTextReader(textReader);
            using var oFileStream = new FileStream(outputPath, fileMode);
            using var dataWriter = new BsonDataWriter(oFileStream);
            dataWriter.WriteToken(jsonReader);
        }
}