2023-03-01 02:19:06 +00:00
|
|
|
//go:build !yq_nojson
|
|
|
|
|
2022-07-27 02:26:22 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jsonDecoder struct {
|
|
|
|
decoder json.Decoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJSONDecoder() Decoder {
|
|
|
|
return &jsonDecoder{}
|
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func (dec *jsonDecoder) Init(reader io.Reader) error {
|
2022-07-27 02:26:22 +00:00
|
|
|
dec.decoder = *json.NewDecoder(reader)
|
2022-10-28 03:16:46 +00:00
|
|
|
return nil
|
2022-07-27 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func (dec *jsonDecoder) Decode() (*CandidateNode, error) {
|
2022-07-27 02:26:22 +00:00
|
|
|
|
2023-04-13 05:40:41 +00:00
|
|
|
var dataBucket CandidateNode
|
2022-07-27 02:26:22 +00:00
|
|
|
err := dec.decoder.Decode(&dataBucket)
|
|
|
|
if err != nil {
|
2022-10-28 03:16:46 +00:00
|
|
|
return nil, err
|
2022-07-27 02:26:22 +00:00
|
|
|
}
|
2023-04-13 05:40:41 +00:00
|
|
|
// log.Debug("convert to yaml")
|
|
|
|
// node, err := dec.convertToYamlNode(&dataBucket)
|
|
|
|
// log.Debug("done, %w", err)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2022-10-28 03:16:46 +00:00
|
|
|
|
|
|
|
return &CandidateNode{
|
2023-04-03 12:25:37 +00:00
|
|
|
Kind: DocumentNode,
|
2023-04-13 05:40:41 +00:00
|
|
|
Content: []*CandidateNode{&dataBucket},
|
2022-10-28 03:16:46 +00:00
|
|
|
}, nil
|
2022-07-27 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:25:37 +00:00
|
|
|
func (dec *jsonDecoder) convertToYamlNode(data *orderedMap) (*CandidateNode, error) {
|
2022-08-01 06:26:43 +00:00
|
|
|
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.
|
|
|
|
return parseSnippet(fmt.Sprintf("%v", rawData))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-03 12:25:37 +00:00
|
|
|
var yamlMap = &CandidateNode{Kind: MappingNode, Tag: "!!map"}
|
2022-08-01 06:26:43 +00:00
|
|
|
for _, keyValuePair := range data.kv {
|
|
|
|
yamlValue, err := dec.convertToYamlNode(&keyValuePair.V)
|
2022-07-27 02:26:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-01 06:26:43 +00:00
|
|
|
yamlMap.Content = append(yamlMap.Content, createScalarNode(keyValuePair.K, keyValuePair.K), yamlValue)
|
2022-07-27 02:26:22 +00:00
|
|
|
}
|
|
|
|
return yamlMap, nil
|
2022-08-01 06:26:43 +00:00
|
|
|
|
2022-07-27 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:25:37 +00:00
|
|
|
func (dec *jsonDecoder) parseArray(dataArray []*orderedMap) (*CandidateNode, error) {
|
2022-07-27 02:26:22 +00:00
|
|
|
|
2023-04-03 12:25:37 +00:00
|
|
|
var yamlMap = &CandidateNode{Kind: 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
|
|
|
|
}
|