2016-11-30 23:08:21 +01:00
|
|
|
/* Generic Reviver, toJSON, and fromJSON functions used for saving and loading objects */
|
|
|
|
|
2021-09-25 04:04:30 +02:00
|
|
|
export interface IReviverValue {
|
|
|
|
ctor: string;
|
|
|
|
data: any;
|
|
|
|
}
|
|
|
|
|
2016-11-30 23:08:21 +01:00
|
|
|
// A generic "smart reviver" function.
|
|
|
|
// Looks for object values with a `ctor` property and
|
|
|
|
// a `data` property. If it finds them, and finds a matching
|
|
|
|
// constructor that has a `fromJSON` property on it, it hands
|
|
|
|
// off to that `fromJSON` fuunction, passing in the value.
|
2021-09-25 07:26:03 +02:00
|
|
|
export function Reviver(key: string, value: IReviverValue | null): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (value == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
console.log("Reviver WRONGLY called with key: " + key + ", and value: " + value);
|
2021-09-05 01:09:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-02-12 10:14:38 +01:00
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
if (typeof value === "object" && typeof value.ctor === "string" && typeof value.data !== "undefined") {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Compatibility for version v0.43.1
|
|
|
|
// TODO Remove this eventually
|
|
|
|
if (value.ctor === "AllServersMap") {
|
|
|
|
console.log("Converting AllServersMap for v0.43.1");
|
|
|
|
return value.data;
|
|
|
|
}
|
2019-02-12 10:14:38 +01:00
|
|
|
|
2021-09-25 04:04:30 +02:00
|
|
|
const ctor = Reviver.constructors[value.ctor];
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (typeof ctor === "function" && typeof ctor.fromJSON === "function") {
|
|
|
|
return ctor.fromJSON(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
2016-11-30 23:08:21 +01:00
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
2021-09-25 04:04:30 +02:00
|
|
|
export namespace Reviver {
|
|
|
|
export const constructors: { [key: string]: any } = {};
|
|
|
|
}
|
2016-11-30 23:08:21 +01:00
|
|
|
|
|
|
|
// A generic "toJSON" function that creates the data expected
|
|
|
|
// by Reviver.
|
|
|
|
// `ctorName` The name of the constructor to use to revive it
|
|
|
|
// `obj` The object being serialized
|
|
|
|
// `keys` (Optional) Array of the properties to serialize,
|
|
|
|
// if not given then all of the objects "own" properties
|
|
|
|
// that don't have function values will be serialized.
|
|
|
|
// (Note: If you list a property in `keys`, it will be serialized
|
|
|
|
// regardless of whether it's an "own" property.)
|
|
|
|
// Returns: The structure (which will then be turned into a string
|
|
|
|
// as part of the JSON.stringify algorithm)
|
2021-09-25 07:26:03 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-09-25 04:04:30 +02:00
|
|
|
export function Generic_toJSON(ctorName: string, obj: any, keys?: string[]): IReviverValue {
|
2016-11-30 23:08:21 +01:00
|
|
|
if (!keys) {
|
|
|
|
keys = Object.keys(obj); // Only "own" properties are included
|
|
|
|
}
|
|
|
|
|
2021-09-25 04:04:30 +02:00
|
|
|
const data: any = {};
|
2019-02-12 10:14:38 +01:00
|
|
|
for (let index = 0; index < keys.length; ++index) {
|
2021-09-25 04:04:30 +02:00
|
|
|
const key = keys[index];
|
2016-11-30 23:08:21 +01:00
|
|
|
data[key] = obj[key];
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return { ctor: ctorName, data: data };
|
2016-11-30 23:08:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A generic "fromJSON" function for use with Reviver: Just calls the
|
|
|
|
// constructor function with no arguments, then applies all of the
|
|
|
|
// key/value pairs from the raw data to the instance. Only useful for
|
|
|
|
// constructors that can be reasonably called without arguments!
|
|
|
|
// `ctor` The constructor to call
|
|
|
|
// `data` The data to apply
|
|
|
|
// Returns: The object
|
2021-09-25 07:26:03 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-09-25 04:04:30 +02:00
|
|
|
export function Generic_fromJSON<T>(ctor: new () => T, data: any): T {
|
|
|
|
const obj: any = new ctor();
|
|
|
|
for (const name in data) {
|
2016-11-30 23:08:21 +01:00
|
|
|
obj[name] = data[name];
|
|
|
|
}
|
|
|
|
return obj;
|
2016-12-01 23:18:18 +01:00
|
|
|
}
|