Skip to content

Commit

Permalink
Fixed number parsing as float bug in JSON #1756
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Sep 18, 2023
1 parent 0a0182d commit e09779e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pkg/yqlib/decoder_json.go
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/json_test.go
Expand Up @@ -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",
},
{
Expand Down

0 comments on commit e09779e

Please sign in to comment.