Skip to content

JSON to YAML: numbers like 1e3 load as strings in PyYAML #237

Closed

Description

@anurag629

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

js-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 .0 if the part before the e has no dot, and + if the exponent has no sign. Then all three parsers read a float. So 1e3 becomes 1.0e+3, 1e-7 becomes 1.0e-7, 1.5e3 becomes 1.5e+3, 1E3 becomes 1.0E+3 and -1e3 becomes -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

Acceptance criteria

  • {"num":1e3,"tiny":1e-7} gives num: 1.0e+3 and tiny: 1.0e-7
  • 1.5e3, 1E3, 2e+5, -1e3, 1.0e3 and 1.5e-3 give 1.5e+3, 1.0E+3, 2.0e+5, -1.0e+3, 1.0e+3 and 1.5e-3
  • 1.2345678901234567890123e5 gives 1.2345678901234567890123e+5, with no digits lost
  • 12, 1.5, -0.5 and 12345678901234567890 don'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 test and npm run build pass

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

  1. Saifjafri212213 commented on Oct 21, 2026

    @Saifjafri212213
    Collaborator

    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:

    • In the number branch of yamlValue (lib/json-utils.ts), when the number text has an exponent, add .0 if the part before the e has no dot and + if the exponent has no sign, so 1e3 becomes 1.0e+3, 1e-7 becomes 1.0e-7, 1.5e3 becomes 1.5e+3, 1E3 becomes 1.0E+3 and -1e3 becomes -1.0e+3
    • Leave numbers without an exponent exactly as they are, so every digit of a big number is still kept
    • Tests for each of the examples in the issue (and 2e+5 and 1.0e3, which are already floats), plus plain, negative and large numbers staying as written; the new cases fail on the current code
    • Check the output with js-yaml and the yaml package, and with PyYAML if I can run it here, then npm run lint, npm run test and npm run build

    I'll branch off develop. Thanks!

  2. anurag629 commented on Oct 21, 2026

    @anurag629
    MemberAuthor

    Hey @Saifjafri212213, go for it! It's yours, next to ogcops #68.

    One thing in your plan: 2e+5 and 1.0e3 aren't floats for PyYAML yet. It needs a dot and a sign in the exponent, so both load as strings, and the criteria want 2.0e+5 and 1.0e+3 for them. Your rule already does that, so just make the tests expect the new text. Let me know if you have any questions.

  3. added a commit that references this issue on Oct 22, 2026
    e8fc92f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workinggood first issueGood for newcomershacktoberfestGood pick for Hacktoberfest contributors

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions