Convert string serialization to new framework

This commit is contained in:
Matt Nadareski
2024-04-03 21:42:08 -04:00
parent 9b1bacd167
commit f2c6fa2b8e
9 changed files with 93 additions and 30 deletions

View File

@@ -4,22 +4,29 @@ This library comprises of serializers that both read and write from files and st
Find the link to the Nuget package [here](https://www.nuget.org/packages/SabreTools.Serialization).
## Interfaces
Below is a table representing the various interfaces that are implemented within this library.
| Interface Name | Source Type | Destination Type |
| --- | --- | --- |
| `IByteDeserializer` | `byte[]?` | Model |
| `IFileDeserializer` | `string?` Path | Model |
| `IFileSerializer` | Model | `string?` Path |
| `IFileDeserializer` | `string?` path | Model |
| `IFileSerializer` | Model | `string?` path |
| `IModelSerializer` | Model | Model |
| `IStreamDeserializer` | `Stream?` | Model |
| `IStreamSerializer` | Model | `Stream?` |
| `IStringDeserializer` | `string?` representation | Model |
| `IStringSerializer` | Model | `string?` representation |
| `IWrapper` | N/A | N/A |
## `SabreTools.Serialization.CrossModel`
## Namespaces
This namespace comprises of serializers and deserializers that convert models to other common ones. This is mainly used for metadata files converting to and from a common, `Dictionary`-based model.
Below is a table of all namespaces within the library and what they represent
## `SabreTools.Serialization.Strings`
This namespace comprises of serializers and deserializers that can convert to and from strings. Most of the serializers are symmetric, but this is not guaranteed. Unimplemented methods will throw `NotImplementedException`.
## `SabreTools.Serialization.Wrappers`
This namespace comrpises of wrapping classes that include keeping a reference to the source of each serializable model. Some of the wrappers may also include what are referred to as "extension properties", which are generated properties derived from either parts of the model or the underlying source.
| Namespace | Description |
| --- | --- |
| `SabreTools.Serialization.CrossModel` | Convert between models; mainly used for metadata files converting to and from a common, `Dictionary`-based model |
| `SabreTools.Serialization.Deserializers` | Convert from external sources to models |
| `SabreTools.Serialization.Serializers` | Convert from models to external sources |
| `SabreTools.Serialization.Wrappers` | Classes that wrap serialization and models to allow for including extension properties |

View File

@@ -1,9 +1,19 @@
using SabreTools.Serialization.Interfaces;
namespace SabreTools.Serialization.Strings
namespace SabreTools.Serialization.Deserializers
{
public partial class XMID : IStringSerializer<Models.Xbox.XMID>
public partial class XMID :
IStringDeserializer<Models.Xbox.XMID>
{
#region IStringDeserializer
/// <inheritdoc cref="IStringDeserializer.Deserialize(string?)"/>
public static Models.Xbox.XMID? DeserializeString(string? str)
{
var deserializer = new XMID();
return deserializer.Deserialize(str);
}
/// <inheritdoc/>
public Models.Xbox.XMID? Deserialize(string? str)
{
@@ -36,5 +46,7 @@ namespace SabreTools.Serialization.Strings
return xmid;
}
#endregion
}
}

View File

@@ -1,9 +1,19 @@
using SabreTools.Serialization.Interfaces;
namespace SabreTools.Serialization.Strings
namespace SabreTools.Serialization.Deserializers
{
public partial class XeMID : IStringSerializer<Models.Xbox.XeMID>
public partial class XeMID :
IStringDeserializer<Models.Xbox.XeMID>
{
#region IStringDeserializer
/// <inheritdoc cref="IStringDeserializer.Deserialize(string?)"/>
public static Models.Xbox.XeMID? DeserializeString(string? str)
{
var deserializer = new XeMID();
return deserializer.Deserialize(str);
}
/// <inheritdoc/>
public Models.Xbox.XeMID? Deserialize(string? str)
{
@@ -60,5 +70,7 @@ namespace SabreTools.Serialization.Strings
return xemid;
}
#endregion
}
}

View File

@@ -0,0 +1,16 @@
namespace SabreTools.Serialization.Interfaces
{
/// <summary>
/// Defines how to serialize from strings
/// </summary>
public interface IStringDeserializer<T>
{
/// <summary>
/// Deserialize a string into <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T">Type of object to deserialize to</typeparam>
/// <param name="str">String to deserialize from</param>
/// <returns>Filled object on success, null on error</returns>
T? Deserialize(string? str);
}
}

View File

@@ -1,18 +1,10 @@
namespace SabreTools.Serialization.Interfaces
{
/// <summary>
/// Defines how to serialize to and from strings
/// Defines how to serialize from strings
/// </summary>
public interface IStringSerializer<T>
{
/// <summary>
/// Deserialize a string into <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T">Type of object to deserialize to</typeparam>
/// <param name="str">String to deserialize from</param>
/// <returns>Filled object on success, null on error</returns>
T? Deserialize(string? str);
/// <summary>
/// Serialize a <typeparamref name="T"/> into a string
/// </summary>

View File

@@ -1,10 +1,20 @@
using System.Text;
using SabreTools.Serialization.Interfaces;
namespace SabreTools.Serialization.Strings
namespace SabreTools.Serialization.Serializers
{
public partial class XMID : IStringSerializer<Models.Xbox.XMID>
public partial class XMID :
IStringSerializer<Models.Xbox.XMID>
{
#region IStringSerializer
/// <inheritdoc cref="IStringSerializer.Serialize(T?)"/>
public static string? SerializeString(Models.Xbox.XMID? obj)
{
var deserializer = new XMID();
return deserializer.Serialize(obj);
}
/// <inheritdoc/>
public string? Serialize(Models.Xbox.XMID? obj)
{
@@ -20,5 +30,7 @@ namespace SabreTools.Serialization.Strings
return sb.ToString();
}
#endregion
}
}

View File

@@ -1,10 +1,20 @@
using System.Text;
using SabreTools.Serialization.Interfaces;
namespace SabreTools.Serialization.Strings
namespace SabreTools.Serialization.Serializers
{
public partial class XeMID : IStringSerializer<Models.Xbox.XeMID>
public partial class XeMID :
IStringSerializer<Models.Xbox.XeMID>
{
#region IStringSerializer
/// <inheritdoc cref="IStringSerializer.Serialize(T?)"/>
public static string? SerializeString(Models.Xbox.XeMID? obj)
{
var deserializer = new XeMID();
return deserializer.Serialize(obj);
}
/// <inheritdoc/>
public string? Serialize(Models.Xbox.XeMID? obj)
{
@@ -26,5 +36,7 @@ namespace SabreTools.Serialization.Strings
return sb.ToString();
}
#endregion
}
}

View File

@@ -87,7 +87,7 @@ namespace SabreTools.Serialization.Wrappers
if (data == null || data.Length == 0)
return null;
var binary = new Strings.XMID().Deserialize(data);
var binary = Deserializers.XMID.DeserializeString(data);
if (binary == null)
return null;

View File

@@ -102,7 +102,7 @@ namespace SabreTools.Serialization.Wrappers
if (data == null || data.Length == 0)
return null;
var binary = new Strings.XeMID().Deserialize(data);
var binary = Deserializers.XeMID.DeserializeString(data);
if (binary == null)
return null;