Skip to content

Commit

Permalink
Set tags on maps,arrays when decoding json #1422
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Nov 15, 2022
1 parent cb609a1 commit fd35530
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/yqlib/decoder_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (dec *jsonDecoder) convertToYamlNode(data *orderedMap) (*yaml.Node, error)
}
}

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

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

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

for _, value := range dataArray {
yamlValue, err := dec.convertToYamlNode(value)
Expand Down
8 changes: 8 additions & 0 deletions pkg/yqlib/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ const roundTripMultiLineJson = `{
`

var jsonScenarios = []formatScenario{
{
description: "set tags",
skipDoc: true,
input: "[{}]",
expression: `[.. | type]`,
scenarioType: "roundtrip-ndjson",
expected: "[\"!!seq\",\"!!map\"]\n",
},
{
description: "Parse json: simple",
subdescription: "JSON is a subset of yaml, so all you need to do is prettify the output",
Expand Down

0 comments on commit fd35530

Please sign in to comment.