From dd08ca69b1fd7228783fbc235eb2e6f6ab63681a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Alvergnat?= Date: Thu, 5 Dec 2019 16:18:08 +0100 Subject: [PATCH 1/2] Add errorHandler option --- lib/serialijse.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/serialijse.js b/lib/serialijse.js index 8de0c1b..a3c62ae 100644 --- a/lib/serialijse.js +++ b/lib/serialijse.js @@ -261,6 +261,9 @@ } var serializingObject = {}; + var _throw = function () { + throw new Error("invalid typeof " + typeof object + " " + JSON.stringify(object, null, " ")); + } switch (typeof object) { case 'number': @@ -272,7 +275,11 @@ _serialize_object(context,serializingObject, object, options); break; default: - throw new Error("invalid typeof " + typeof object + " " + JSON.stringify(object, null, " ")); + if (options.errorHandler) { + options.errorHandler(context, options, object, _throw) + } else { + _throw() + } } return serializingObject; From fc24ba0553f31a112e13c9086d8a7a6ed77ae84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Alvergnat?= Date: Thu, 5 Dec 2019 16:31:21 +0100 Subject: [PATCH 2/2] Enhance TypeScript definition file --- lib/serialijse.d.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/serialijse.d.ts b/lib/serialijse.d.ts index 72b5f50..f785c1c 100644 --- a/lib/serialijse.d.ts +++ b/lib/serialijse.d.ts @@ -1,5 +1,17 @@ declare module "serialijse" { - export function serialize(object: T, options?: object): string; + export type IgnoreSpec = string | RegExp + + export interface SerializeContext { + index: any[], + objects: any[] + } + + export interface SerializeOptions { + ignored?: IgnoreSpec[]|IgnoreSpec + errorHandler?: (context: SerializeContext, options: SerializeOptions, object: any, _throw: () => any) => any; + } + + export function serialize(object: T, options?: SerializeOptions): string; export function deserialize(serializationString: string): T;