tc39 / proposal-decimal

Built-in exact decimal numbers for JavaScript
http://tc39.es/proposal-decimal/
497 stars 18 forks source link

Throw when JSON.stringify works with decimals #167

Closed jessealama closed 2 months ago

jessealama commented 4 months ago

As a first step in the integration between JSON and decimals, let's make decimal align with BigInts by throwing a TypeError.

This decision isn't necessarily final. The change is made so that, at least, we have some kind of defined behavior. We may end up returning a value instead of throwing.

CC @sffc

sffc commented 4 months ago

Seems okay, though if we do this then the urgency of toString returning the full data model (#12) is elevated, since developers will reach for toString when serializing to JSON.

safinaskar commented 4 months ago

Of course, JSON.stringify should convert decimals to string. I. e. if we have decimal embedded in some object, then JSON.stringify should return something like {"foo":"0.1"}. Because this is common practice to represent monetary decimals in REST APIs as strings

sffc commented 4 months ago

I would be slightly in favor, I think, of Decimal supporting toJSON, but if it does, it should retain the whole data model (trailing zeros) in order for it to round-trip.