question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON.stringify does not allow returning undefined

See original GitHub issue

Calling JSON.stringify with undefined, or a function returns undefined in most contexts (browsers)

val x = scala.scalajs.js.JSON.stringify(())

println(s"found: $x" )

see: https://scalafiddle.io/sf/oFq7bkS/0 to reproduce.

I believe there are two directions to go, either change the return type to UndefOr[String] or document that this function may throw an exception.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sjrdcommented, Jun 22, 2020

I agree with @gzm0 here. At the end of the day, it is a bit of a judgment call what uses of an API are considered “valid”. Very old JavaScript APIs like JSON are very permissive, often more for legacy reasons than careful design. If we always type the most permissive interpretation of those APIs, the expected, regular usages become very clunky. It becomes a trade-off.

In some cases like js.Array, we make an intentional decision to restrict the facades to “valid” use cases, for some definition of “valid” that we chose. In the case of JSON.stringify, I don’t remember it being an intentional decision when we added it, but I think that sticking to String is reasonable.

0reactions
ritschwummcommented, Jun 23, 2020

@gzm0 good point

now, i wonder if i can make use of the type system to prevent passing invalid values to JSON.stringify…

Read more comments on GitHub >

github_iconTop Results From Across the Web

When stringify doesn't return a string - Evan Hahn
I think it's surprising that JSON.stringify can ever return anything other than a string. But it can return undefined in 6 cases:.
Read more >
Why JSON.stringify returns undefined - Stack Overflow
In your row you have indexes, not values. better solution: rows.forEach(row => console.log(JSON.stringify(row.data)));.
Read more >
JSON.stringify removes undefined, how to keep it
JSON.stringify will omit all object attributes that are undefined . In most cases, it doesn't really matter, because if we parse that string ......
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
JSON.stringify() can return undefined when passing in "pure" values like JSON.stringify(() => {}) or JSON.stringify(undefined) .
Read more >
The second argument in JSON.stringify() - Dillion's Blog
From the above code, we returned undefined for the id property. This means the id property will not be included in the output....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found