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

Remove circular references when stringifying an object #11

Merged
merged 5 commits into from
Sep 13, 2021

Conversation

hbcarlos
Copy link
Contributor

Remove circular references when stringifying an object because otherwise, the JSON.stringify throws an error.

@jtpio
Copy link
Member

jtpio commented Sep 10, 2021

@@ -169,13 +169,31 @@ const extension: JupyterFrontEndPlugin<void> = {
const _trace = console.trace;
const _table = console.table;

// https://stackoverflow.com/a/11616993
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering whether we want to take this approach though.

As mentioned in the SA answer:

The replacer in this example is not 100% correct (depending on your definition of "duplicate"). In the following case, a value is discarded:

This indeed discards some of the elements:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a warning message informing the user about the possibility of missing attributes

@jtpio jtpio added the bug Something isn't working label Sep 10, 2021
@hbcarlos
Copy link
Contributor Author

@jtpio
Copy link
Member

jtpio commented Sep 13, 2021

Nice!

src/index.ts Outdated
Comment on lines 405 to 413
const msg =
'This error contains a object with a circular reference. During the process of removing the reference we could have removed duplicated attributes.\n';
const obj = JSON.stringify(arg, refReplacer);
cache = [];
console.error(msg, obj);
data += obj;
} catch (e) {
data += ' ';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could do a quick refactor if this piece of code is the same in all cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh, thanks. I forgot about that.
I'm on it.

src/index.ts Outdated Show resolved Hide resolved
Copy link
Member

@jtpio jtpio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jtpio
Copy link
Member

jtpio commented Sep 13, 2021

Let's wait for CI and a quick check on Binder and this should be good to go.

@jtpio
Copy link
Member

jtpio commented Sep 13, 2021

Binder seems to be having issues right now.

Just pulled locally and it looks good 👍

image

@jtpio jtpio merged commit 9e01c55 into main Sep 13, 2021
@jtpio jtpio deleted the circular-references branch September 13, 2021 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants