Set tags on maps,arrays when decoding json #1422

This commit is contained in:
Mike Farah 2022-11-15 11:42:31 +11:00
parent cb609a1886
commit fd35530f35
2 changed files with 10 additions and 2 deletions

View File

@ -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 { for _, keyValuePair := range data.kv {
yamlValue, err := dec.convertToYamlNode(&keyValuePair.V) yamlValue, err := dec.convertToYamlNode(&keyValuePair.V)
if err != nil { if err != nil {
@ -74,7 +74,7 @@ func (dec *jsonDecoder) convertToYamlNode(data *orderedMap) (*yaml.Node, error)
func (dec *jsonDecoder) parseArray(dataArray []*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 { for _, value := range dataArray {
yamlValue, err := dec.convertToYamlNode(value) yamlValue, err := dec.convertToYamlNode(value)

View File

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