From e09779e0042f1aec21a416212664e60518017002 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 19 Sep 2023 09:42:07 +1000 Subject: [PATCH] Fixed number parsing as float bug in JSON #1756 --- pkg/yqlib/decoder_json.go | 12 ++++++++++-- pkg/yqlib/json_test.go | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/yqlib/decoder_json.go b/pkg/yqlib/decoder_json.go index c6b88eca..ea712bfc 100644 --- a/pkg/yqlib/decoder_json.go +++ b/pkg/yqlib/decoder_json.go @@ -54,8 +54,16 @@ func (dec *jsonDecoder) convertToYamlNode(data *orderedMap) (*yaml.Node, error) case nil: return createScalarNode(nil, "null"), nil case float64, float32: - // json decoder returns ints as float. - return parseSnippet(fmt.Sprintf("%v", rawData)) + // json decoder returns ints as float.' + intNum := int(rawData.(float64)) + + // if the integer representation is the same as the original + // then its an int. + if float64(intNum) == rawData.(float64) { + return createScalarNode(intNum, fmt.Sprintf("%v", intNum)), nil + } + + return createScalarNode(rawData, fmt.Sprintf("%v", rawData)), nil case int, int64, int32, string, bool: return createScalarNode(rawData, fmt.Sprintf("%v", rawData)), nil case []*orderedMap: diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go index 919fce9f..cda716ba 100644 --- a/pkg/yqlib/json_test.go +++ b/pkg/yqlib/json_test.go @@ -198,8 +198,8 @@ var jsonScenarios = []formatScenario{ { description: "numbers", skipDoc: true, - input: "[3, 3.0, 3.1, -1]", - expected: "- 3\n- 3\n- 3.1\n- -1\n", + input: "[3, 3.0, 3.1, -1, 999999, 1000000, 1000001, 1.1]", + expected: "- 3\n- 3\n- 3.1\n- -1\n- 999999\n- 1000000\n- 1000001\n- 1.1\n", scenarioType: "decode-ndjson", }, {