2023-03-01 02:19:06 +00:00
|
|
|
//go:build !yq_nojson
|
|
|
|
|
2020-11-17 05:17:38 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
|
|
)
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func yamlToJSON(t *testing.T, sampleYaml string, indent int) string {
|
|
|
|
t.Helper()
|
2020-11-17 05:17:38 +00:00
|
|
|
var output bytes.Buffer
|
|
|
|
writer := bufio.NewWriter(&output)
|
|
|
|
|
2024-02-24 04:59:12 +00:00
|
|
|
prefs := ConfiguredJSONPreferences.Copy()
|
2024-02-24 04:48:59 +00:00
|
|
|
prefs.Indent = indent
|
|
|
|
prefs.UnwrapScalar = false
|
|
|
|
var jsonEncoder = NewJSONEncoder(prefs)
|
2022-10-28 03:16:46 +00:00
|
|
|
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
|
2020-11-17 05:17:38 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
node := inputs.Front().Value.(*CandidateNode)
|
|
|
|
log.Debugf("%v", NodeToString(node))
|
|
|
|
// log.Debugf("Content[0] %v", NodeToString(node.Content[0]))
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
err = jsonEncoder.Encode(writer, node)
|
2020-11-19 11:11:26 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-17 05:17:38 +00:00
|
|
|
writer.Flush()
|
|
|
|
|
2021-01-17 06:09:22 +00:00
|
|
|
return strings.TrimSuffix(output.String(), "\n")
|
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
func TestJSONEncoderPreservesObjectOrder(t *testing.T) {
|
2021-01-17 06:09:22 +00:00
|
|
|
var sampleYaml = `zabbix: winner
|
|
|
|
apple: great
|
|
|
|
banana:
|
|
|
|
- {cobra: kai, angus: bob}
|
|
|
|
`
|
2022-02-07 00:55:55 +00:00
|
|
|
var expectedJSON = `{
|
2021-01-17 06:09:22 +00:00
|
|
|
"zabbix": "winner",
|
|
|
|
"apple": "great",
|
|
|
|
"banana": [
|
|
|
|
{
|
|
|
|
"cobra": "kai",
|
|
|
|
"angus": "bob"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`
|
2023-10-18 01:11:53 +00:00
|
|
|
var actualJSON = yamlToJSON(t, sampleYaml, 2)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, expectedJSON, actualJSON)
|
2021-01-17 06:09:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 02:37:21 +00:00
|
|
|
func TestJsonNullInArray(t *testing.T) {
|
|
|
|
var sampleYaml = `[null]`
|
2023-10-18 01:11:53 +00:00
|
|
|
var actualJSON = yamlToJSON(t, sampleYaml, 0)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, sampleYaml, actualJSON)
|
2021-10-30 02:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonNull(t *testing.T) {
|
|
|
|
var sampleYaml = `null`
|
2023-10-18 01:11:53 +00:00
|
|
|
var actualJSON = yamlToJSON(t, sampleYaml, 0)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, sampleYaml, actualJSON)
|
2021-10-30 02:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonNullInObject(t *testing.T) {
|
|
|
|
var sampleYaml = `{x: null}`
|
2023-10-18 01:11:53 +00:00
|
|
|
var actualJSON = yamlToJSON(t, sampleYaml, 0)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, `{"x":null}`, actualJSON)
|
2021-10-30 02:37:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 06:09:22 +00:00
|
|
|
func TestJsonEncoderDoesNotEscapeHTMLChars(t *testing.T) {
|
|
|
|
var sampleYaml = `build: "( ./lint && ./format && ./compile ) < src.code"`
|
2022-02-07 00:55:55 +00:00
|
|
|
var expectedJSON = `{"build":"( ./lint && ./format && ./compile ) < src.code"}`
|
2023-10-18 01:11:53 +00:00
|
|
|
var actualJSON = yamlToJSON(t, sampleYaml, 0)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, expectedJSON, actualJSON)
|
2020-11-17 05:17:38 +00:00
|
|
|
}
|