JSON.stringify does not allow returning undefined
See original GitHub issueCalling 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:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 ofJSON.stringify
, I don’t remember it being an intentional decision when we added it, but I think that sticking toString
is reasonable.@gzm0 good point
now, i wonder if i can make use of the type system to prevent passing invalid values to JSON.stringify…