@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
32 lines (24 loc) • 837 B
JavaScript
/**
* @template T
* @param {T} json
* @param {Map<string, function>} deserializers
*/
export function abstractJSONDeserializer(json, deserializers) {
if (json === null || typeof json !== "object") {
// primitive type
return json;
}
const typeName = json.type;
if (typeof typeName !== "string") {
throw new Error(`Expected json.typeName to be a string, instead was '${typeof typeName}'(=${typeName})`);
}
const deserializer = deserializers.get(typeName);
if (deserializer === undefined) {
throw new Error(`No deserializer found for type '${typeName}'`);
}
const payload = json.data;
if (payload === undefined) {
console.warn(`No serialized payload for type '${typeName}'`);
}
return deserializer(payload);
}