Skip to content

Commit fd35530

Browse files
committed
Set tags on maps,arrays when decoding json #1422
1 parent cb609a1 commit fd35530

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

pkg/yqlib/decoder_json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (dec *jsonDecoder) convertToYamlNode(data *orderedMap) (*yaml.Node, error)
6060
}
6161
}
6262

63-
var yamlMap = &yaml.Node{Kind: yaml.MappingNode}
63+
var yamlMap = &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
6464
for _, keyValuePair := range data.kv {
6565
yamlValue, err := dec.convertToYamlNode(&keyValuePair.V)
6666
if err != nil {
@@ -74,7 +74,7 @@ func (dec *jsonDecoder) convertToYamlNode(data *orderedMap) (*yaml.Node, error)
7474

7575
func (dec *jsonDecoder) parseArray(dataArray []*orderedMap) (*yaml.Node, error) {
7676

77-
var yamlMap = &yaml.Node{Kind: yaml.SequenceNode}
77+
var yamlMap = &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
7878

7979
for _, value := range dataArray {
8080
yamlValue, err := dec.convertToYamlNode(value)

pkg/yqlib/json_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ const roundTripMultiLineJson = `{
7878
`
7979

8080
var jsonScenarios = []formatScenario{
81+
{
82+
description: "set tags",
83+
skipDoc: true,
84+
input: "[{}]",
85+
expression: `[.. | type]`,
86+
scenarioType: "roundtrip-ndjson",
87+
expected: "[\"!!seq\",\"!!map\"]\n",
88+
},
8189
{
8290
description: "Parse json: simple",
8391
subdescription: "JSON is a subset of yaml, so all you need to do is prettify the output",

0 commit comments

Comments
 (0)