Refactor JSON serialiable class pattern

Change the JSON seriasiable data classes to use an explicit static
fromJSON method and an optional toJSON method for turning them to and
from JSON.

This improves over the previous pattern of passing the JSON to the
constructor as it frees up the constructor to be able to take whichever
argument types suits the code instead of being beholden to the JSON
serialised form.
This commit is contained in:
Hornwitser committed 2023-02-25 00:02:12 +01:00
1 parent a448d87e3a
commit 9a967a5779
20 files changed
+222 -97

No files matched your search

+2 -2
View File
@@ -385,7 +385,7 @@ class ControlConnection extends BaseConnection {
}
async createModPackRequestHandler(message) {
let modPack = new libData.ModPack(message.data.mod_pack);
let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
if (this._controller.modPacks.has(modPack.id)) {
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} already exist`);
}
@@ -394,7 +394,7 @@ class ControlConnection extends BaseConnection {
}
async updateModPackRequestHandler(message) {
let modPack = new libData.ModPack(message.data.mod_pack);
let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
if (!this._controller.modPacks.has(modPack.id)) {
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} does not exist`);
}
+1 -1
View File
@@ -413,7 +413,7 @@ class Controller {
}
return new Map();
}
return new Map(json.map(e => [e.id, new libData.ModPack(e)]));
return new Map(json.map(e => [e.id, libData.ModPack.fromJSON(e)]));
}
static async saveModPacks(filePath, modPacks) {
+3 -5
View File
@@ -26,11 +26,9 @@ class InstanceInfo {
*/
status;
// This is not actually json as a config can't be loaded synchronously but
// that is likely to change in the future.
constructor(json) {
this.config = json["config"];
this.status = json["status"];
constructor({ config, status }) {
this.config = config;
this.status = status;
}
toJSON() {
+1 -1
View File
@@ -232,7 +232,7 @@ async function uploadExport(req, res) {
await libFileOps.safeOutputFile(path.join("static", `${name}.${hash}${ext}`), await file.async("nodebuffer"));
}
modPack.exportManifest = new libData.ExportManifest({ assets });
modPack.exportManifest = new libData.ExportManifest(assets);
modPack.fillDefaultSettings(settingPrototypes, logger);
res.app.locals.controller.modPackUpdated(modPack);