JSON.stringify makes bigints and decimals to strings

734 views
Skip to first unread message

andreas andersson

unread,
Jul 22, 2018, 5:46:21 AM7/22/18
to DataStax Node.js Driver for Apache Cassandra Mailing List
Hi,

If I'm running JSON.stringify on a Row that contains bigints/decimals they become strings. How to workaround this? I've tried to send in my own replaces to stringify but the types are already string there. 

Any ideas?

Cheers
Andreas

Andy Tolbert

unread,
Jul 23, 2018, 10:53:54 AM7/23/18
to nodejs-dr...@lists.datastax.com
Hi Andreas,

Since javascript treats JSON numbers as double-precision floats, it can't represent all CQL bigint and decimal values as javascript Numbers.  When converting into JSON, we override the toJSON methods to return the toString representation (Long, BigDecimal) as opposed to their object content. 

We could change that to return Number in the case that the value can be represented as a Number, and String where it couldn't, but it wouldn't be a complete solution.  Eventually you'll encounter a value that cannot be represented as a number, in which case, you'll get a JSON string.  I think it is easier for clients to only worry about parsing the value as one type, instead of checking if its a Number or String then do the conversion.

Alternatively, the JSON specification does not define bounds for Number, so you could argue that it might be reasonable to render these values as numbers, but unfortunately I do not think there is a way to achieve this with javascript using JSON.stringify so you would have to come up with a custom solution.

Thanks,
Andy

--
You received this message because you are subscribed to the Google Groups "DataStax Node.js Driver for Apache Cassandra Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs-driver-user+unsub...@lists.datastax.com.

andreas andersson

unread,
Aug 1, 2018, 2:37:22 PM8/1/18
to DataStax Node.js Driver for Apache Cassandra Mailing List
Hi Andy,

Thanks for the response, then I know :)

/ Andreas
Reply all
Reply to author
Forward
0 new messages