node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

JSON Column type will lose precision when include bigint

Open sheldonzf opened this issue 3 years ago • 4 comments

if mysql column is { "TemplateId": 7088992364139642880 }

when I query used mysql2.

It wil be { "TemplateId": 7088992364139642000 }.

And I found text_parse.js, It used JSON.parse not JSONBig, So it would lose precision when occurs bigint. image

sheldonzf avatar Sep 02 '22 16:09 sheldonzf

Personally I think everyone who uses JSON as a serialization format have to assume IEEE 754 double precision on number serialization, anything that can potentially out of that range should use string

We could add a flag to skip automatic JSON.parse by driver and return strings for JSON columns, in that case its a client choice what to use to deserialize

In fact, with .query() this is possible right now with typeCast config

sidorares avatar Sep 03 '22 07:09 sidorares

potential solution: a config option to allow to pass custom JSON.parse function

sidorares avatar Mar 11 '23 07:03 sidorares

The above solution would be great. I am working on upgrading several repos to use json-bigint. Instead of JSON.parse, I would pass in JSONbig.parse.

will-malisch avatar May 25 '23 20:05 will-malisch

Also ran into this, I think if the supportBigNumbers flag is set in the config it'd make sense to support parsing BigInts in JSON

RhysSullivan avatar Sep 24 '23 07:09 RhysSullivan