diff --git a/Files/Catalog.Deserializer.cs b/Files/Catalog.Deserializer.cs new file mode 100644 index 00000000..05d9345a --- /dev/null +++ b/Files/Catalog.Deserializer.cs @@ -0,0 +1,11 @@ +using System.Text; + +namespace SabreTools.Serialization.Files +{ + public partial class Catalog : JsonFile + { + // Catalog.js file is a UTF-16 LE JSON + public new Models.Xbox.Catalog? Deserialize(string? path) + => Deserialize(path, new UnicodeEncoding()); + } +} diff --git a/Files/Catalog.Serializer.cs b/Files/Catalog.Serializer.cs new file mode 100644 index 00000000..7066bcc7 --- /dev/null +++ b/Files/Catalog.Serializer.cs @@ -0,0 +1,11 @@ +using System.Text; + +namespace SabreTools.Serialization.Files +{ + public partial class Catalog : JsonFile + { + // Catalog.js file is a UTF-16 LE JSON + public new bool Serialize(Models.Xbox.Catalog? obj, string? path) + => Serialize(obj, path, new UnicodeEncoding()); + } +} diff --git a/Files/JsonFile.Deserializer.cs b/Files/JsonFile.Deserializer.cs new file mode 100644 index 00000000..0cd62dfe --- /dev/null +++ b/Files/JsonFile.Deserializer.cs @@ -0,0 +1,31 @@ +using System.Text; +using SabreTools.Serialization.Interfaces; + +namespace SabreTools.Serialization.Files +{ + /// + /// Base class for other JSON serializers + /// + /// + public partial class JsonFile : IFileSerializer + { + /// + public T? Deserialize(string? path) + => Deserialize(path, new UTF8Encoding(false)); + + /// + /// Deserialize a file into + /// + /// Type of object to deserialize to + /// Path to deserialize from + /// Encoding to parse text as + /// Filled object on success, null on error + public T? Deserialize(string? path, Encoding encoding) + { + using (var data = PathProcessor.OpenStream(path)) + { + return new Streams.JsonFile().Deserialize(data, encoding); + } + } + } +} diff --git a/Files/JsonFile.Serializer.cs b/Files/JsonFile.Serializer.cs new file mode 100644 index 00000000..17994e9f --- /dev/null +++ b/Files/JsonFile.Serializer.cs @@ -0,0 +1,40 @@ +using System.IO; +using System.Text; +using SabreTools.Serialization.Interfaces; + +namespace SabreTools.Serialization.Files +{ + /// + /// Base class for other JSON serializers + /// + /// + public partial class JsonFile : IFileSerializer + { + /// + public bool Serialize(T? obj, string? path) + => Serialize(obj, path, new UTF8Encoding(false)); + + /// + /// Serialize a into a file + /// + /// Type of object to serialize from + /// Data to serialize + /// Path to the file to serialize to + /// Encoding to parse text as + /// True on successful serialization, false otherwise + public bool Serialize(T? obj, string? path, Encoding encoding) + { + if (string.IsNullOrEmpty(path)) + return false; + + using var stream = new Streams.JsonFile().Serialize(obj, encoding); + if (stream == null) + return false; + + using var fs = File.OpenWrite(path); + stream.CopyTo(fs); + + return true; + } + } +} diff --git a/Files/XmlFile.Serializer.cs b/Files/XmlFile.Serializer.cs index c332e754..19fdea26 100644 --- a/Files/XmlFile.Serializer.cs +++ b/Files/XmlFile.Serializer.cs @@ -18,7 +18,6 @@ namespace SabreTools.Serialization.Files /// /// Data to serialize /// Path to the file to serialize to - /// Data to serialize /// Optional DOCTYPE name /// Optional DOCTYPE pubid /// Optional DOCTYPE sysid diff --git a/SabreTools.Serialization.csproj b/SabreTools.Serialization.csproj index 7c1a17e2..8dabf2a5 100644 --- a/SabreTools.Serialization.csproj +++ b/SabreTools.Serialization.csproj @@ -8,7 +8,7 @@ latest enable true - 1.4.1 + 1.4.2 Matt Nadareski @@ -27,8 +27,9 @@ + - + diff --git a/Streams/Catalog.Deserializer.cs b/Streams/Catalog.Deserializer.cs new file mode 100644 index 00000000..42250c21 --- /dev/null +++ b/Streams/Catalog.Deserializer.cs @@ -0,0 +1,12 @@ +using System.IO; +using System.Text; + +namespace SabreTools.Serialization.Streams +{ + public partial class Catalog : JsonFile + { + // Catalog JSON is encoded as UTF-16 LE + public new Models.Xbox.Catalog? Deserialize(Stream? data) + => Deserialize(data, new UnicodeEncoding()); + } +} diff --git a/Streams/Catalog.Serializer.cs b/Streams/Catalog.Serializer.cs new file mode 100644 index 00000000..0739a679 --- /dev/null +++ b/Streams/Catalog.Serializer.cs @@ -0,0 +1,12 @@ +using System.IO; +using System.Text; + +namespace SabreTools.Serialization.Streams +{ + public partial class Catalog : JsonFile + { + // Catalog JSON is encoded as UTF-16 LE + public new Stream? Serialize(Models.Xbox.Catalog? obj) + => Serialize(obj, new UnicodeEncoding()); + } +} diff --git a/Streams/JsonFile.Deserializer.cs b/Streams/JsonFile.Deserializer.cs new file mode 100644 index 00000000..9467376a --- /dev/null +++ b/Streams/JsonFile.Deserializer.cs @@ -0,0 +1,40 @@ +using System.IO; +using System.Text; +using Newtonsoft.Json; +using SabreTools.Serialization.Interfaces; + +namespace SabreTools.Serialization.Streams +{ + /// + /// Base class for other JSON serializers + /// + /// + public partial class JsonFile : IStreamSerializer + { + /// + public T? Deserialize(Stream? data) + => Deserialize(data, new UTF8Encoding(false)); + + /// + /// Deserialize a Stream into + /// + /// Type of object to deserialize to + /// Stream to parse + /// Text encoding to use + /// Filled object on success, null on error + 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(jsonReader); + } + } +} diff --git a/Streams/JsonFile.Serializer.cs b/Streams/JsonFile.Serializer.cs new file mode 100644 index 00000000..4cf8405d --- /dev/null +++ b/Streams/JsonFile.Serializer.cs @@ -0,0 +1,43 @@ +using System.IO; +using System.Text; +using Newtonsoft.Json; +using SabreTools.Serialization.Interfaces; + +namespace SabreTools.Serialization.Streams +{ + /// + /// Base class for other JSON serializers + /// + /// + public partial class JsonFile : IStreamSerializer + { + /// + public Stream? Serialize(T? obj) + => Serialize(obj, new UTF8Encoding(false)); + + /// + /// Serialize a into a Stream + /// + /// Type of object to serialize from + /// Data to serialize + /// + /// Filled object on success, null on error + 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; + } + } +} diff --git a/Streams/XmlFile.Serializer.cs b/Streams/XmlFile.Serializer.cs index 0de1957e..292a5c25 100644 --- a/Streams/XmlFile.Serializer.cs +++ b/Streams/XmlFile.Serializer.cs @@ -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 {