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

safeDump crashes with obscure "unknown reason" if JSON contains undefined #325

Closed
pelotom opened this issue Jan 31, 2017 · 3 comments
Closed

Comments

@pelotom
Copy link

pelotom commented Jan 31, 2017

For example:

yaml.safeDump({ foo: undefined })

throws

(unknown reason)

      at writeNode (node_modules/js-yaml/lib/js-yaml/dumper.js:731:13)
      at writeBlockMapping (node_modules/js-yaml/lib/js-yaml/dumper.js:610:10)
      at writeNode (node_modules/js-yaml/lib/js-yaml/dumper.js:703:9)
      at dump (node_modules/js-yaml/lib/js-yaml/dumper.js:792:7)
      at Object.safeDump (node_modules/js-yaml/lib/js-yaml/dumper.js:798:10)
      ...

It's not clear what should happen, but any of these seem like better alternatives:

  • Treat the key as absent if undefined is found as a value in an object
  • Treat it the same as null
  • Make the error message more descriptive: "Encountered undefined value in JSON" or somesuch
@puzrin
Copy link
Member

puzrin commented Jan 31, 2017

undefined is not valid value for JSON & YAML. You passed JS object, not JSON.

But i agree, dumper could be more nice.

@dervus ?

@MunifTanjim
Copy link

MunifTanjim commented Apr 28, 2018

JSON.stringify removes the key-pair with undefined value.

yaml.safeDump should do the same.

@rlidwka
Copy link
Member

rlidwka commented Dec 18, 2020

Fixed in 56d5616 (currently in dev branch).

Now undefined follows roughly the same black magic as in JSON:

  • replaced with null in collections (errors out if null isn't in the schema)
  • removed from mappings entirely if either key or value is undefined

If user defines custom schema for undefined, it serializes as before.

@rlidwka rlidwka closed this as completed Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants