UNPKG

4.51 kBSource Map (JSON)View Raw
1{"version":3,"file":"serializable.js","sourceRoot":"","sources":["../../../src/serializable.ts"],"names":[],"mappings":";;;AAQA,yCAA2C;AAc9B,QAAA,wBAAwB,GAAG,gBAAgB,CAAC;AA0EzD,SAAgB,iBAAiB,CAAI,IAAS,EAAE,IAAqB;IACjE,OAAO,gBAAM,CAAC,WAAW,CAAI,IAAI,EAAE,IAAI,EAAE,EAAE,wBAAwB,EAAE,gCAAwB,EAAE,CAAC,CAAC;AACrG,CAAC;AAFD,8CAEC;AAED,SAAgB,eAAe,CAAI,GAAM;IACrC,OAAO,gBAAM,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,wBAAwB,EAAE,gCAAwB,EAAE,CAAY,CAAC;AACpG,CAAC;AAFD,0CAEC","sourcesContent":["// ==LICENSE-BEGIN==\n// Copyright 2017 European Digital Reading Lab. All rights reserved.\n// Licensed to the Readium Foundation under one or more contributor license agreements.\n// Use of this source code is governed by a BSD-style license\n// that can be found in the LICENSE file exposed on Github (readium) in the project repository.\n// ==LICENSE-END==\n\n// https://github.com/edcarroll/ta-json\nimport { JSON as TAJSON } from \"ta-json-x\";\n\n// import * as debug_ from \"debug\";\n// const debug = debug_(\"r2:lcp/serializable\");\n\nexport type AnyJson = JsonPrimitives | JsonArray | JsonMap;\nexport type JsonPrimitives = string | number | boolean | null;\nexport interface JsonMap {\n [key: string]: AnyJson;\n}\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface JsonArray extends Array<AnyJson> {\n}\n\nexport const KeyToPreserveUnknownJSON = \"AdditionalJSON\";\n\nexport interface IWithAdditionalJSON {\n [KeyToPreserveUnknownJSON]: JsonMap | undefined;\n // SupportedKeys: string[] | undefined;\n // parseAdditionalJSON: (json: JsonMap) => void;\n // generateAdditionalJSON: (json: JsonMap) => void;\n}\n// export function parseAdditionalJSON(obj: IWithAdditionalJSON, json: JsonMap) {\n// if (!json || typeof json !== \"object\") {\n// return;\n// }\n// Object.keys(json).forEach((key) => {\n// if (json.hasOwnProperty(key)) {\n// if (obj.SupportedKeys && obj.SupportedKeys.includes(key)) {\n// return;\n// }\n// if (!obj.AdditionalJSON) {\n// obj.AdditionalJSON = {};\n// }\n// // warning: reference copy, not deep clone!\n// obj.AdditionalJSON[key] = json[key];\n// }\n// });\n// }\n// export function generateAdditionalJSON(obj: IWithAdditionalJSON, json: JsonMap) {\n// if (!json || typeof json !== \"object\") {\n// return;\n// }\n// if (!obj.AdditionalJSON) {\n// return;\n// }\n// const keys = Object.keys(obj.AdditionalJSON);\n// for (const key of keys) {\n// if (obj.AdditionalJSON.hasOwnProperty(key)) {\n// if (obj.SupportedKeys && obj.SupportedKeys.includes(key)) {\n// continue;\n// }\n// // warning: reference copy, not deep clone!\n// json[key] = obj.AdditionalJSON[key];\n// }\n// }\n// }\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype TConstructor<T> = new(value?: any) => T;\n// type TFunction<T> = ObjectConstructor[\"constructor\"] & TConstructor<T>;\n\n// // tslint:disable-next-line: max-line-length\n// export function TaJsonDeserialize<T extends IWithAdditionalJSON>(json: any, type: TConstructor<T>): T {\n// // debug(\"TaJsonDeserialize 1\");\n// // debug(json);\n// const obj = TAJSON.deserialize<T>(json, type, { keyToPreserveUnknownJSON: \"AdditionalJSON\" });\n// // debug(\"TaJsonDeserialize 2\");\n// // debug(obj);\n// // obj.parseAdditionalJSON(json);\n// // debug(\"TaJsonDeserialize 3\");\n// // debug(obj);\n// return obj;\n// }\n\n// export function TaJsonSerialize<T extends IWithAdditionalJSON>(obj: T): JsonMap {\n// // debug(\"TaJsonSerialize 1\");\n// // debug(obj);\n// const json = TAJSON.serialize(obj, { keyToPreserveUnknownJSON: \"AdditionalJSON\" }) as JsonMap;\n// // debug(\"TaJsonSerialize 2\");\n// // debug(json);\n// // obj.generateAdditionalJSON(json);\n// // debug(\"TaJsonSerialize 3\");\n// // debug(json);\n// return json;\n// }\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function TaJsonDeserialize<T>(json: any, type: TConstructor<T>): T {\n return TAJSON.deserialize<T>(json, type, { keyToPreserveUnknownJSON: KeyToPreserveUnknownJSON });\n}\n\nexport function TaJsonSerialize<T>(obj: T): JsonMap {\n return TAJSON.serialize(obj, { keyToPreserveUnknownJSON: KeyToPreserveUnknownJSON }) as JsonMap;\n}\n"]}
\No newline at end of file