Files
SabreTools.Serialization/SabreTools.Serialization.Writers/JsonFile.cs

106 lines
3.5 KiB
C#
Raw Normal View History

using System.IO;
using System.Text;
using Newtonsoft.Json;
2025-10-27 22:43:56 -04:00
using SabreTools.IO.Extensions;
2025-09-26 14:59:45 -04:00
namespace SabreTools.Serialization.Writers
{
/// <summary>
/// Base class for other JSON serializers
/// </summary>
/// <typeparam name="T"></typeparam>
public class JsonFile<T> : BaseBinaryWriter<T>
{
#region IByteWriter
2024-04-04 02:42:25 -04:00
/// <inheritdoc/>
public override byte[]? SerializeArray(T? obj)
=> SerializeArray(obj, new UTF8Encoding(false));
/// <summary>
/// Serialize a <typeparamref name="T"/> into a byte array
/// </summary>
/// <typeparam name="T">Type of object to serialize from</typeparam>
/// <param name="obj">Data to serialize</param>
/// <param name="encoding">Encoding to parse text as</param>
/// <returns>Filled object on success, null on error</returns>
public byte[]? SerializeArray(T? obj, Encoding encoding)
{
using var stream = Serialize(obj, encoding);
2026-01-25 14:30:18 -05:00
if (stream is null)
2024-04-04 02:42:25 -04:00
return null;
byte[] bytes = new byte[stream.Length];
2024-11-13 02:42:14 -05:00
int read = stream.Read(bytes, 0, bytes.Length);
2024-04-04 02:42:25 -04:00
return bytes;
}
#endregion
#region IFileWriter
2024-04-03 21:27:50 -04:00
/// <inheritdoc/>
2025-09-26 10:20:48 -04:00
public override bool SerializeFile(T? obj, string? path)
2024-04-03 21:27:50 -04:00
=> Serialize(obj, path, new UTF8Encoding(false));
/// <summary>
/// Serialize a <typeparamref name="T"/> into a file
/// </summary>
/// <typeparam name="T">Type of object to serialize from</typeparam>
/// <param name="obj">Data to serialize</param>
/// <param name="path">Path to the file to serialize to</param>
/// <param name="encoding">Encoding to parse text as</param>
/// <returns>True on successful serialization, false otherwise</returns>
public bool Serialize(T? obj, string? path, Encoding encoding)
{
if (string.IsNullOrEmpty(path))
return false;
using var stream = Serialize(obj, encoding);
2026-01-25 14:30:18 -05:00
if (stream is null)
2024-04-03 21:27:50 -04:00
return false;
using var fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
2026-03-24 19:17:25 -04:00
stream.BlockCopy(fs);
fs.Flush();
2024-04-03 21:27:50 -04:00
return true;
}
#endregion
#region IStreamWriter
2024-04-03 21:27:50 -04:00
/// <inheritdoc/>
2025-09-26 10:20:48 -04:00
public override Stream? SerializeStream(T? obj)
2024-04-03 16:35:54 -04:00
=> Serialize(obj, new UTF8Encoding(false));
/// <summary>
/// Serialize a <typeparamref name="T"/> into a Stream
/// </summary>
/// <typeparam name="T">Type of object to serialize from</typeparam>
/// <param name="obj">Data to serialize</param>
/// <param name="encoding"></param>
/// <returns>Filled object on success, null on error</returns>
2024-04-03 16:35:54 -04:00
public Stream? Serialize(T? obj, Encoding encoding)
{
// If the object is null
2026-01-25 14:30:18 -05:00
if (obj is null)
return null;
// Setup the serializer and the writer
var serializer = JsonSerializer.Create();
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream, encoding);
var jsonWriter = new JsonTextWriter(streamWriter);
// Perform the deserialization and return
serializer.Serialize(jsonWriter, obj);
2025-10-27 22:43:56 -04:00
stream.SeekIfPossible(0, SeekOrigin.Begin);
return stream;
}
2024-04-03 21:27:50 -04:00
#endregion
}
}