Skip to main content

Module js_number

Module js_number 

Source
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:

ValueJSON.stringifyserde_json
100.0100100.0
1e201000000000000000000001e20
1e-60.0000011e-6
-0.00-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 f64 exactly as ECMAScript’s String(x) / JSON.stringify(x) would.