mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-04-27 08:39:47 +00:00
* Add JSON/catalog.js logic * Proper json deserialize * Update packages * Catalog is UTF-16 LE, make BaseJsonFile encoding independent * Bump version, use ST.Models 1.4.1 * Implement JsonFile as interface with UTF8 as default * typo
41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using SabreTools.Serialization.Interfaces;
|
|
|
|
namespace SabreTools.Serialization.Files
|
|
{
|
|
/// <summary>
|
|
/// Base class for other JSON serializers
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public partial class JsonFile<T> : IFileSerializer<T>
|
|
{
|
|
/// <inheritdoc/>
|
|
public bool Serialize(T? obj, string? path)
|
|
=> 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 = new Streams.JsonFile<T>().Serialize(obj, encoding);
|
|
if (stream == null)
|
|
return false;
|
|
|
|
using var fs = File.OpenWrite(path);
|
|
stream.CopyTo(fs);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|