Files
yq/pkg/yqlib
ChrisJr404andGitHub 2861815f71 fix(json): preserve floats with trailing zero when encoding YAML to JSON (#2701)
YAML scalars tagged `!!float` were round-tripped through `float64` and
re-serialized by Go's JSON encoder, which strips the decimal part of
whole-number floats. As a result, `50.0` came out as `50` and a
sequence like `[50.0, 95.0, 99.0, 99.9]` became `[50,95,99,99.9]`,
turning a uniform array of floats into a mixed int/float array that
downstream consumers (Horreum, JSON Schema validators, jq, etc.)
reject.

The JSON spec does not distinguish ints from floats, but every common
JSON library (Go's `encoding/json`, Python's `json`, jq) preserves the
fractional form of values that came in as floats. yq's YAML decoder
already parses these as `!!float` with the original text intact, so we
can emit them verbatim instead of round-tripping.

`MarshalJSON` for `ScalarNode` now special-cases `!!float`:
- if `Value` is already a JSON-shaped number literal containing a `.`
  or exponent, emit it verbatim (e.g. `50.0`, `99.9`, `1.5e-3`, `-7.0`);
- if `Value` is an integer-shaped string tagged `!!float` (e.g.
  `!!float 5`), format the parsed float and append `.0` so it stays a
  JSON number with a fractional part;
- otherwise (empty value, parse error, or non-finite result), fall back
  to the existing encoding path so behaviour for `.inf` / `.nan` and
  anything unusual is unchanged.

`!!int` nodes still encode as JSON integers.

Closes #2683

Signed-off-by: ChrisJr404 <chris@hacknow.com>
2026-05-14 20:00:34 +10:00
..
2023-10-18 12:11:53 +11:00
2025-12-20 19:11:48 +11:00
2023-10-18 12:11:53 +11:00
2023-10-18 12:11:53 +11:00
2026-02-01 10:27:18 +11:00
2025-12-20 19:11:48 +11:00
2025-05-17 18:55:53 +10:00
2024-02-24 15:48:59 +11:00
2025-05-03 16:34:21 +10:00
2025-06-07 15:23:38 +10:00
2026-02-01 10:27:18 +11:00
2025-12-08 20:30:47 +11:00
2025-05-17 18:55:53 +10:00
2025-05-17 18:55:53 +10:00
2024-02-24 15:59:12 +11:00
2026-04-06 19:29:07 +10:00
2024-02-24 15:36:16 +11:00
2023-10-18 12:11:53 +11:00
2025-12-08 20:30:47 +11:00
2023-10-03 16:22:23 +11:00
2025-12-26 11:08:24 +11:00
2023-10-18 12:11:53 +11:00
2024-01-11 13:17:34 +11:00
2023-10-18 12:11:53 +11:00
2023-10-18 12:11:53 +11:00
2023-10-18 12:11:53 +11:00
2024-01-11 13:17:34 +11:00
2023-10-18 12:11:53 +11:00
2023-10-18 12:11:53 +11:00
2024-11-16 11:48:53 +11:00
2023-10-18 12:11:53 +11:00
2024-01-11 13:17:34 +11:00
2025-11-09 16:12:17 +11:00
2025-11-09 16:12:17 +11:00
2023-10-18 12:11:53 +11:00
2023-10-02 14:43:12 +11:00
2024-01-11 13:17:34 +11:00
2023-03-16 13:39:36 +11:00
2024-01-11 13:17:34 +11:00
2023-10-18 12:11:53 +11:00
2024-01-11 13:17:34 +11:00
2025-05-03 16:34:21 +10:00
2025-05-03 16:34:21 +10:00
2025-05-03 16:34:21 +10:00
2023-10-18 12:11:53 +11:00
2024-03-30 13:29:35 +11:00
2024-01-11 13:17:34 +11:00
2024-01-11 13:17:34 +11:00
2023-10-18 12:11:53 +11:00
2025-06-07 15:23:38 +10:00
2023-10-18 12:11:53 +11:00
2024-01-11 13:17:34 +11:00
2023-10-18 12:11:53 +11:00
2025-06-07 15:23:38 +10:00
2024-06-15 17:06:17 +10:00
2023-10-18 12:11:53 +11:00
2026-02-01 10:27:18 +11:00
2025-12-20 19:11:48 +11:00
2025-05-03 16:34:21 +10:00
2024-02-24 15:14:21 +11:00
2024-02-21 14:50:55 +11:00
2025-12-14 19:41:45 +11:00
2024-02-24 15:03:30 +11:00
2025-12-06 19:08:37 +11:00