Expand description
ECMAScript Number::toString(x, 10), which is what JSON.stringify uses for numbers.
This exists because serde_json formats f64 with ryu, producing the shortest
round-tripping decimal, and that is not the same string ECMAScript produces. Parse
Server is a Node server, so its number output is ECMAScript’s, and wire compatibility
means matching it byte for byte.
Measured divergences between ryu and JSON.stringify, all reproduced in the tests below:
| Value | JSON.stringify | serde_json |
|---|---|---|
100.0 | 100 | 100.0 |
1e20 | 100000000000000000000 | 1e20 |
1e-6 | 0.000001 | 1e-6 |
-0.0 | 0 | -0.0 |
Not a divergence, despite looking like one: both emit + in a positive exponent, so
1.5e+300 already matches. Do not “fix” that.
Spec: ECMA-262 §6.1.6.1.20, Number::toString.
Note the distinction this module does not cover: number representation, meaning which
BSON type a value is stored as, is a separate problem handled at the storage boundary in
parse-rust-mongo. Conflating the two is a mistake this project made once.
Functions§
- to_
ecma_ string - Format an
f64exactly as ECMAScript’sString(x)/JSON.stringify(x)would.