Skip to content

Commit

Permalink
Merge pull request #11 from Toilal/more-options
Browse files Browse the repository at this point in the history
More options
  • Loading branch information
erossignon committed May 2, 2021
2 parents 69641bc + fc24ba0 commit fb85298
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/serialijse.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
declare module "serialijse" {
export function serialize<T>(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<T>(object: T, options?: SerializeOptions): string;

export function deserialize<T>(serializationString: string): T;

Expand Down
9 changes: 8 additions & 1 deletion lib/serialijse.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@
}

var serializingObject = {};
var _throw = function () {
throw new Error("invalid typeof " + typeof object + " " + JSON.stringify(object, null, " "));
}

switch (typeof object) {
case 'number':
Expand All @@ -350,7 +353,11 @@
_serialize_object(context, options, serializingObject, object);
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;
}
Expand Down

0 comments on commit fb85298

Please sign in to comment.