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

Including a p5.Vector in saveJSON throws a circular structure error #5470

Closed
chrsmlls333 opened this issue Nov 7, 2021 · 5 comments
Closed

Comments

@chrsmlls333
Copy link

Submitting an object with a p5.Vector object throws a TypeError. Test code and associated error:

let v = createVector(4,4);
saveJSON({ v }, 'test');
Uncaught TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'p5'
    |     property '_curElement' -> object with constructor '_main.default.Renderer2D'
    --- property '_pixelsState' closes the circle
    at JSON.stringify (<anonymous>)
    at p5._main.default.saveJSON (p5.js:85038)
    at <anonymous>:1:1

I think two solutions are possible:

  1. Filter out any p5 object properties transparent to the end user.

    This would involve assessing p5.Vector and any other built-in classes for error-throwing content and making a custom replacer function to replace these with simplified or string equivalents.

  2. Expand the saveJSON arguments to include a user-provided replacer.

    Here is some possible code for this:

p5.prototype.saveJSON = function(json, filename, opt, replacer) {
  p5._validateParameters('saveJSON', arguments);
  let stringify;
  if (opt) {
    stringify = JSON.stringify(json, replacer);
  } else {
    stringify = JSON.stringify(json, replacer, 2);
  }
  this.saveStrings(stringify.split('\n'), filename, 'json');
};
  1. Both of the above

    We could make a basic replacer and then allow a user to provide their own to either compound or override the filter.

Location of code edit:

p5.prototype.saveJSON = function(json, filename, opt) {

@chrsmlls333
Copy link
Author

Oh, there is already a pull request to solve the vector issue specifically. #5469

Still, this may be present for other structures?

@limzykenneth
Copy link
Member

#5469 will solve this problem for p5.Vector. Cyclic reference is still in the p5 instance itself and it will not be possible to be refactor out easily. I haven't come across anywhere else where it is a problem yet but if you find one let us know and we can look into it.

@chrsmlls333
Copy link
Author

Would it make sense to filter out p5 instances here regardless? That could be a blanket fix for beginners' accessibility.

@limzykenneth
Copy link
Member

What do you mean?

@limzykenneth
Copy link
Member

Resolved by #5469

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants