Skip to content

Commit

Permalink
De/Serialize XboxOne/XSX catalog.js files (#6)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Deterous authored Apr 2, 2024
1 parent d265c14 commit 69e1f5f
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 4 deletions.
11 changes: 11 additions & 0 deletions Files/Catalog.Deserializer.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions Files/Catalog.Serializer.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions Files/JsonFile.Deserializer.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions Files/JsonFile.Serializer.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 0 additions & 1 deletion Files/XmlFile.Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public bool Serialize(T? obj, string? path)
/// </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>
Expand Down
5 changes: 3 additions & 2 deletions SabreTools.Serialization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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 changes: 12 additions & 0 deletions Streams/Catalog.Deserializer.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions Streams/Catalog.Serializer.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions Streams/JsonFile.Deserializer.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions Streams/JsonFile.Serializer.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion Streams/XmlFile.Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial class XmlFile<T> : IStreamSerializer<T>
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
{
Expand Down

0 comments on commit 69e1f5f

Please sign in to comment.