-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
De/Serialize XboxOne/XSX catalog.js files (#6)
* 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
Showing
11 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters