yq/pkg/yqlib/decoder_json.go

101 lines
2.3 KiB
Go
Raw Normal View History

//go:build !yq_nojson
2022-07-27 02:26:22 +00:00
package yqlib
import (
"fmt"
"io"
"github.com/goccy/go-json"
yaml "gopkg.in/yaml.v3"
)
type jsonDecoder struct {
decoder json.Decoder
}
func NewJSONDecoder() Decoder {
return &jsonDecoder{}
}
func (dec *jsonDecoder) Init(reader io.Reader) error {
2022-07-27 02:26:22 +00:00
dec.decoder = *json.NewDecoder(reader)
return nil
2022-07-27 02:26:22 +00:00
}
func (dec *jsonDecoder) Decode() (*CandidateNode, error) {
2022-07-27 02:26:22 +00:00
var dataBucket orderedMap
2022-07-27 02:26:22 +00:00
log.Debug("going to decode")
err := dec.decoder.Decode(&dataBucket)
if err != nil {
return nil, err
2022-07-27 02:26:22 +00:00
}
node, err := dec.convertToYamlNode(&dataBucket)
2022-07-27 02:26:22 +00:00
if err != nil {
return nil, err
2022-07-27 02:26:22 +00:00
}
return &CandidateNode{
Node: &yaml.Node{
Kind: yaml.DocumentNode,
Content: []*yaml.Node{node},
},
}, nil
2022-07-27 02:26:22 +00:00
}
func (dec *jsonDecoder) convertToYamlNode(data *orderedMap) (*yaml.Node, error) {
if data == nil {
return createScalarNode(nil, "null"), nil
}
if data.kv == nil {
switch rawData := data.altVal.(type) {
case nil:
return createScalarNode(nil, "null"), nil
case float64, float32:
// 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:
return dec.parseArray(rawData)
default:
return nil, fmt.Errorf("unrecognised type :( %v", rawData)
}
2022-07-27 02:26:22 +00:00
}
var yamlMap = &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
2023-08-28 07:39:46 +00:00
for i, keyValuePair := range data.kv {
yamlValue, err := dec.convertToYamlNode(&data.kv[i].V)
2022-07-27 02:26:22 +00:00
if err != nil {
return nil, err
}
yamlMap.Content = append(yamlMap.Content, createScalarNode(keyValuePair.K, keyValuePair.K), yamlValue)
2022-07-27 02:26:22 +00:00
}
return yamlMap, nil
2022-07-27 02:26:22 +00:00
}
func (dec *jsonDecoder) parseArray(dataArray []*orderedMap) (*yaml.Node, error) {
2022-07-27 02:26:22 +00:00
var yamlMap = &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
2022-07-27 02:26:22 +00:00
for _, value := range dataArray {
yamlValue, err := dec.convertToYamlNode(value)
if err != nil {
return nil, err
}
yamlMap.Content = append(yamlMap.Content, yamlValue)
}
return yamlMap, nil
}