mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-02-04 05:36:12 +00:00
Finish serialization for Archive.org
This commit is contained in:
7
Files/ArchiveDotOrg.Serializer.cs
Normal file
7
Files/ArchiveDotOrg.Serializer.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SabreTools.Serialization.Files
|
||||
{
|
||||
public partial class ArchiveDotOrg : XmlFile<Models.ArchiveDotOrg.Files>
|
||||
{
|
||||
// All serialization logic is in the base class
|
||||
}
|
||||
}
|
||||
53
Files/XmlFile.Serializer.cs
Normal file
53
Files/XmlFile.Serializer.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SabreTools.Serialization.Files
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for other XML serializers
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public partial class XmlFile<T> : IFileSerializer<T>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
#if NET48
|
||||
public bool Serialize(T obj, string path)
|
||||
#else
|
||||
public bool Serialize(T? obj, string? path)
|
||||
#endif
|
||||
=> Serialize(obj, path, null, null, null, null);
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the defined type to an XML file
|
||||
/// </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>
|
||||
/// <param name="subset">Optional DOCTYPE name</param>
|
||||
/// <returns>True on successful serialization, false otherwise</returns>
|
||||
#if NET48
|
||||
protected static bool Serialize(T obj, string path, string name = null, string pubid = null, string sysid = null, string subset = null)
|
||||
#else
|
||||
protected static bool Serialize(T? obj, string? path, string? name = null, string? pubid = null, string? sysid = null, string? subset = null)
|
||||
#endif
|
||||
{
|
||||
using (var stream = new Streams.XmlFile<T>().Serialize(obj, name, pubid, sysid, subset))
|
||||
{
|
||||
if (stream == null)
|
||||
return false;
|
||||
|
||||
using (var fs = File.OpenWrite(path))
|
||||
{
|
||||
stream.CopyTo(fs);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,5 +16,18 @@ namespace SabreTools.Serialization
|
||||
#else
|
||||
T? Deserialize(string? path);
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Sserialize 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>
|
||||
/// <returns>True on successful serialization, false otherwise</returns>
|
||||
#if NET48
|
||||
bool Serialize(T obj, string path);
|
||||
#else
|
||||
bool Serialize(T? obj, string? path);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,34 @@
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines how to serialize to and from files
|
||||
/// Defines how to serialize to and from models
|
||||
/// </summary>
|
||||
public interface IFileSerializer<T, U>
|
||||
public interface IModelSerializer<T, U>
|
||||
{
|
||||
/// <summary>
|
||||
/// Deserialize a <typeparamref name="T"/> into <typeparamref name="u"/>
|
||||
/// Deserialize a <typeparamref name="U"/> into <typeparamref name="T"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of object to deserialize from</typeparam>
|
||||
/// <typeparam name="U">Type of object to deserialize to</typeparam>
|
||||
/// <typeparam name="T">Type of object to deserialize to</typeparam>
|
||||
/// <typeparam name="U">Type of object to deserialize from</typeparam>
|
||||
/// <param name="obj">Object to deserialize from</param>
|
||||
/// <returns>Filled object on success, null on error</returns>
|
||||
#if NET48
|
||||
U Deserialize(T obj);
|
||||
T Deserialize(U obj);
|
||||
#else
|
||||
U? Deserialize(T? obj);
|
||||
T? Deserialize(U? obj);
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Serialize a <typeparamref name="T"/> into <typeparamref name="U"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of object to serialize from</typeparam>
|
||||
/// <typeparam name="U">Type of object to serialize to</typeparam>
|
||||
/// <param name="obj">Object to serialize from</param>
|
||||
/// <returns>Filled object on success, null on error</returns>
|
||||
#if NET48
|
||||
U Serialize(T obj);
|
||||
#else
|
||||
U? Serialize(T? obj);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -18,5 +18,17 @@ namespace SabreTools.Serialization
|
||||
#else
|
||||
T? Deserialize(Stream? data);
|
||||
#endif
|
||||
|
||||
/// <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>
|
||||
/// <returns>Filled object on success, null on error</returns>
|
||||
#if NET48
|
||||
Stream Serialize(T obj);
|
||||
#else
|
||||
Stream? Serialize(T? obj);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,8 @@
|
||||
<!-- Assembly Properties -->
|
||||
<TargetFrameworks>net48;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<RuntimeIdentifiers>win-x86;win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
|
||||
<Version>1.1.1</Version>
|
||||
<Version>1.1.0</Version>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
|
||||
<!-- Package Properties -->
|
||||
<Authors>Matt Nadareski</Authors>
|
||||
|
||||
7
Streams/ArchiveDotOrg.Serializer.cs
Normal file
7
Streams/ArchiveDotOrg.Serializer.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SabreTools.Serialization.Streams
|
||||
{
|
||||
public partial class ArchiveDotOrg : XmlFile<Models.ArchiveDotOrg.Files>
|
||||
{
|
||||
// All serialization logic is in the base class
|
||||
}
|
||||
}
|
||||
69
Streams/XmlFile.Serializer.cs
Normal file
69
Streams/XmlFile.Serializer.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SabreTools.Serialization.Streams
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for other XML serializers
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public partial class XmlFile<T> : IStreamSerializer<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes the defined type to a stream
|
||||
/// </summary>
|
||||
/// <param name="obj">Data to serialize</param>
|
||||
/// <returns>Stream containing serialized data on success, null otherwise</returns>
|
||||
#if NET48
|
||||
public static Stream Serialize(T obj)
|
||||
#else
|
||||
public static Stream? Serialize(T? obj)
|
||||
#endif
|
||||
=> Serialize(obj, null, null, null, null);
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the defined type to a stream
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// <param name="subset">Optional DOCTYPE name</param>
|
||||
/// <returns>Stream containing serialized data on success, null otherwise</returns>
|
||||
#if NET48
|
||||
protected static Stream Serialize(T obj, string name = null, string pubid = null, string sysid = null, string subset = null)
|
||||
#else
|
||||
protected static Stream? Serialize(T? obj, string? name = null, string? pubid = null, string? sysid = null, string? subset = null)
|
||||
#endif
|
||||
{
|
||||
// If the object is null
|
||||
if (obj == null)
|
||||
return null;
|
||||
|
||||
// Setup the serializer and the reader
|
||||
var serializer = new XmlSerializer(typeof(T));
|
||||
var settings = new XmlWriterSettings
|
||||
{
|
||||
CheckCharacters = false,
|
||||
Encoding = Encoding.UTF8,
|
||||
Indent = true,
|
||||
IndentChars = "\t",
|
||||
NewLineChars = "\n",
|
||||
};
|
||||
var stream = new MemoryStream();
|
||||
var streamWriter = new StreamWriter(stream);
|
||||
var xmlWriter = XmlWriter.Create(streamWriter, settings);
|
||||
|
||||
// Write the doctype if provided
|
||||
if (!string.IsNullOrWhiteSpace(name))
|
||||
xmlWriter.WriteDocType(name, pubid, sysid, subset);
|
||||
|
||||
// Perform the deserialization and return
|
||||
serializer.Serialize(xmlWriter, obj);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user