Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More options #11

Merged
merged 2 commits into from May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/serialijse.d.ts
@@ -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
Expand Up @@ -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':
Expand All @@ -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;
Expand Down