Skip to content

Fix JSON reader rejecting exponent notation without a fraction - #560

Open
VXNCXNX wants to merge 1 commit into
TomWright:masterfrom
VXNCXNX:fix/json-exponent-numbers
Open

Fix JSON reader rejecting exponent notation without a fraction#560
VXNCXNX wants to merge 1 commit into
TomWright:masterfrom
VXNCXNX:fix/json-exponent-numbers

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

What's broken

Valid JSON numbers in exponent notation without a decimal point are rejected outright.

Repro

$ echo '{"a":1e3}' | dasel -i json -o json
before: dasel: error: error reading input: could not decode object:
        strconv.ParseInt: parsing "1e3": invalid syntax
after:  {"a": 1000}

$ echo '{"a":1E-2}' | dasel -i json -o json
before: same error on "1E-2"
after:  {"a": 0.01}

2e+2 fails the same way.

The fix

decodeToken only treated a number as a float when the literal contained a ., so the exponent forms went to ParseInt. It now also checks for e or E. Those are the only other characters json.Number can hold, so there is no false positive.

Verification

TestJSONReader_ExponentNumbers covers positive, negative and signed exponents, a fraction with an exponent, and plain integers staying ints. Reverting to the . check fails it on the parse error.

I compared binaries built before and after across the number forms rather than reasoning from the diff: 1, -3, 0, 1.0, 1.5 and an 18 digit integer all produce byte-identical output. Only the three exponent forms change, from an error to a value.

One consequence worth naming: 1e3 now reads as a float, so -o yaml gives !!float 1000. The YAML reader already did exactly that for 1e3 on master, so this aligns the two readers rather than introducing a new behaviour.

go test ./... passes across all 21 packages. gofmt and go vet clean. CHANGELOG entry added under Unreleased/Fixed.

A number was only treated as a float when the literal contained a '.',
so 1e3 and 1E-2 were handed to ParseInt and failed. Both are valid JSON,
and the YAML reader already classifies them as floats.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant