3 ms·
Afaict, JSON serialization is not supported yet, got a "TypeError: BigInt value can't be serialized in JSON". I'm on Firefox Developer edition 68.0b3.
by wpq0 7y ago
Afaict, JSON serialization is not supported yet, got a "TypeError: BigInt value can't be serialized in JSON". I'm on Firefox Developer edition 68.0b3.
- M2Ys4U 7y agoI suppose one could use a replacer[0] when calling JSON.stringify and a reviver[1] when calling JSON.parse [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- bpye 7y agoYou can certainly serialize to a string but knowing when you should revive to a BigInt seems tricky. You could prefix the strings I guess?
- tzs 7y agoHow about serializing to an array ["bigint", "170141183460469231731687303715884105727"]? It takes more space, but is easy to recognize, and easy to generalize to other types.
- jfk13 7y agoPresumably that could be confusing if you ever happen to have an actual array whose first element is the string "bigint".
- hombre_fatal 7y agoWell, no matter what you do, surely you plan on documenting your API to such a bare bones level that your user isn't racking their brain whether a key is a number or an array of data.
- repsilat 7y agoNah, you just have to also encode all arrays like ["array", [...]] ;-)
- derfniw 7y agoReuse the literal? (postfix with 'n')
- bufferoverflow 7y agoYou don't want strings that are "digits..n" to automatically deserialize into bigints. What if you have a string like "8n", which is a tractor model, or "9n", which is a smartphone model? But it would be nice to have literal 8n to convert to a bigint 8n. That's not in the JSON standard though.
- M2Ys4U 7y agoTrue, but if you control both the serialisation and deserialisation you can get away with it