Repository navigation
Description
The JSON formatter's YAML conversion writes each number exactly as it appears in the JSON. That keeps every digit, but it goes wrong for exponents:
{"num":1e3,"tiny":1e-7}gives
num: 1e3
tiny: 1e-7js-yaml and the yaml package read both as numbers, but PyYAML loads {'num': '1e3', 'tiny': '1e-7'}, two strings. A YAML 1.1 float (https://yaml.org/type/float.html) needs a dot and a signed exponent, and PyYAML follows that, so 1.5e3, 1E3, 2e+5, -1e3 and 1.0e3 come back as strings too. A Python script that reads the converted file gets text where the JSON had a number.
Checked with js-yaml 4.3, yaml 2.9 and PyYAML 6.0. #202 made the YAML output safe for YAML 1.1 parsers where strings are concerned, and this is the same problem for numbers.
The number branch of yamlValue (lib/json-utils.ts, line 378) returns the text unchanged:
if (isLosslessNumber(value)) return value.toString();What to do
- When a number has an exponent, add
.0if the part before theehas no dot, and+if the exponent has no sign. Then all three parsers read a float. So1e3becomes1.0e+3,1e-7becomes1.0e-7,1.5e3becomes1.5e+3,1E3becomes1.0E+3and-1e3becomes-1.0e+3. - Keep the digits as they are otherwise. Work on the text from
value.toString()instead of going through a JS number, so long values don't lose digits. - Numbers without an exponent (
12,1.5,12345678901234567890) don't change.
Where to look
lib/json-utils.ts:yamlValue(line 375) and its number line (line 378)lib/__tests__/json-utils.test.ts: thetoYAMLtests (line 244)- JSON to YAML: control characters are written raw, so PyYAML and js-yaml can't read the output #234 (control characters in JSON to YAML) changes
needsQuoteandyamlStringin the same file. The two changes don't overlap, but whoever merges second may need to rebase.
Acceptance criteria
-
{"num":1e3,"tiny":1e-7}givesnum: 1.0e+3andtiny: 1.0e-7 -
1.5e3,1E3,2e+5,-1e3,1.0e3and1.5e-3give1.5e+3,1.0E+3,2.0e+5,-1.0e+3,1.0e+3and1.5e-3 -
1.2345678901234567890123e5gives1.2345678901234567890123e+5, with no digits lost -
12,1.5,-0.5and12345678901234567890don't change - Tests check the output text for each case above (please don't add a YAML parser as a dependency)
-
npm run lint,npm run testandnpm run buildpass
Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off develop, and open your PR against develop with Closes #237 in the description.
Activity
Saifjafri212213 commentedon Oct 21, 2026
Hi @anurag629! I have 8 merged PRs across your repos now (the latest is chatcops #292), and I'd like to take this one. Could you assign it to me? I also have a pending ask on ogcops #68, and I'll finish whichever you assign first before starting the next.
My plan:
yamlValue(lib/json-utils.ts), when the number text has an exponent, add.0if the part before theehas no dot and+if the exponent has no sign, so1e3becomes1.0e+3,1e-7becomes1.0e-7,1.5e3becomes1.5e+3,1E3becomes1.0E+3and-1e3becomes-1.0e+32e+5and1.0e3, which are already floats), plus plain, negative and large numbers staying as written; the new cases fail on the current codeyamlpackage, and with PyYAML if I can run it here, thennpm run lint,npm run testandnpm run buildI'll branch off
develop. Thanks!anurag629 commentedon Oct 21, 2026
Hey @Saifjafri212213, go for it! It's yours, next to ogcops #68.
One thing in your plan:
2e+5and1.0e3aren't floats for PyYAML yet. It needs a dot and a sign in the exponent, so both load as strings, and the criteria want2.0e+5and1.0e+3for them. Your rule already does that, so just make the tests expect the new text. Let me know if you have any questions.fix(json): write YAML numbers with an exponent as floats PyYAML can r…