mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-02-05 13:49:52 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69e1f5ff0b |
11
Files/Catalog.Deserializer.cs
Normal file
11
Files/Catalog.Deserializer.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Text;
|
||||
|
||||
namespace SabreTools.Serialization.Files
|
||||
{
|
||||
public partial class Catalog : JsonFile<Models.Xbox.Catalog>
|
||||
{
|
||||
// Catalog.js file is a UTF-16 LE JSON
|
||||
public new Models.Xbox.Catalog? Deserialize(string? path)
|
||||
=> Deserialize(path, new UnicodeEncoding());
|
||||
}
|
||||
}
|
||||
11
Files/Catalog.Serializer.cs
Normal file
11
Files/Catalog.Serializer.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Text;
|
||||
|
||||
namespace SabreTools.Serialization.Files
|
||||
{
|
||||
public partial class Catalog : JsonFile<Models.Xbox.Catalog>
|
||||
{
|
||||
// Catalog.js file is a UTF-16 LE JSON
|
||||
public new bool Serialize(Models.Xbox.Catalog? obj, string? path)
|
||||
=> Serialize(obj, path, new UnicodeEncoding());
|
||||
}
|
||||
}
|
||||
31
Files/JsonFile.Deserializer.cs
Normal file
31
Files/JsonFile.Deserializer.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
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 T? Deserialize(string? path)
|
||||
=> Deserialize(path, new UTF8Encoding(false));
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize a file into <typeparamref name="T"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of object to deserialize to</typeparam>
|
||||
/// <param name="path">Path to deserialize from</param>
|
||||
/// <param name="encoding">Encoding to parse text as</param>
|
||||
/// <returns>Filled object on success, null on error</returns>
|
||||
public T? Deserialize(string? path, Encoding encoding)
|
||||
{
|
||||
using (var data = PathProcessor.OpenStream(path))
|
||||
{
|
||||
return new Streams.JsonFile<T>().Deserialize(data, encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
40
Files/JsonFile.Serializer.cs
Normal file
40
Files/JsonFile.Serializer.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ namespace SabreTools.Serialization.Files
|
||||
/// </summary>
|
||||
/// <param name="obj">Data to serialize</param>
|
||||
/// <param name="path">Path to the file to serialize to</param>
|
||||
/// <param name="obj">Data to serialize</param>
|
||||
/// <param name="name">Optional DOCTYPE name</param>
|
||||
/// <param name="pubid">Optional DOCTYPE pubid</param>
|
||||
/// <param name="sysid">Optional DOCTYPE sysid</param>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<Version>1.4.1</Version>
|
||||
<Version>1.4.2</Version>
|
||||
|
||||
<!-- Package Properties -->
|
||||
<Authors>Matt Nadareski</Authors>
|
||||
@@ -27,8 +27,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="SabreTools.IO" Version="1.3.2" />
|
||||
<PackageReference Include="SabreTools.Models" Version="1.4.0" />
|
||||
<PackageReference Include="SabreTools.Models" Version="1.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
12
Streams/Catalog.Deserializer.cs
Normal file
12
Streams/Catalog.Deserializer.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SabreTools.Serialization.Streams
|
||||
{
|
||||
public partial class Catalog : JsonFile<Models.Xbox.Catalog>
|
||||
{
|
||||
// Catalog JSON is encoded as UTF-16 LE
|
||||
public new Models.Xbox.Catalog? Deserialize(Stream? data)
|
||||
=> Deserialize(data, new UnicodeEncoding());
|
||||
}
|
||||
}
|
||||
12
Streams/Catalog.Serializer.cs
Normal file
12
Streams/Catalog.Serializer.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SabreTools.Serialization.Streams
|
||||
{
|
||||
public partial class Catalog : JsonFile<Models.Xbox.Catalog>
|
||||
{
|
||||
// Catalog JSON is encoded as UTF-16 LE
|
||||
public new Stream? Serialize(Models.Xbox.Catalog? obj)
|
||||
=> Serialize(obj, new UnicodeEncoding());
|
||||
}
|
||||
}
|
||||
40
Streams/JsonFile.Deserializer.cs
Normal file
40
Streams/JsonFile.Deserializer.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Streams/JsonFile.Serializer.cs
Normal file
43
Streams/JsonFile.Serializer.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
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 Stream? Serialize(T? obj)
|
||||
=> 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>
|
||||
public Stream? Serialize(T? obj, Encoding encoding)
|
||||
{
|
||||
// If the object is null
|
||||
if (obj == 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);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ namespace SabreTools.Serialization.Streams
|
||||
if (obj == null)
|
||||
return null;
|
||||
|
||||
// Setup the serializer and the reader
|
||||
// Setup the serializer and the writer
|
||||
var serializer = new XmlSerializer(typeof(T));
|
||||
var settings = new XmlWriterSettings
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user