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.4 KiB
C#
41 lines
1.4 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
using SabreTools.Serialization.Interfaces;
|
|
|
|
namespace SabreTools.Serialization.Streams
|
|
{
|
|
/// <summary>
|
|
/// Base class for other JSON serializers
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public partial class JsonFile<T> : IStreamSerializer<T>
|
|
{
|
|
/// <inheritdoc/>
|
|
public T? Deserialize(Stream? data)
|
|
=> Deserialize(data, new UTF8Encoding(false));
|
|
|
|
/// <summary>
|
|
/// Deserialize a Stream into <typeparamref name="T"/>
|
|
/// </summary>
|
|
/// <typeparam name="T">Type of object to deserialize to</typeparam>
|
|
/// <param name="data">Stream to parse</param>
|
|
/// <param name="encoding">Text encoding to use</param>
|
|
/// <returns>Filled object on success, null on error</returns>
|
|
public T? Deserialize(Stream? data, Encoding encoding)
|
|
{
|
|
// If the stream is null
|
|
if (data == null)
|
|
return default;
|
|
|
|
// Setup the serializer and the reader
|
|
var serializer = JsonSerializer.Create();
|
|
var streamReader = new StreamReader(data, encoding);
|
|
var jsonReader = new JsonTextReader(streamReader);
|
|
|
|
// Perform the deserialization and return
|
|
return serializer.Deserialize<T>(jsonReader);
|
|
}
|
|
}
|
|
}
|