From 867bef0f03c7d5dcd4e397a8deaada249ee8106e Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Tue, 13 May 2025 18:45:22 +0800 Subject: [PATCH] feat(yaml): add JSON style encoding option to NewEncoder (#2038) --- pkg/yaml/yaml.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/yaml/yaml.go b/pkg/yaml/yaml.go index 1b30da25..588bbc4f 100644 --- a/pkg/yaml/yaml.go +++ b/pkg/yaml/yaml.go @@ -20,7 +20,12 @@ type Encoder interface { // NewEncoder creates and returns a function that is used to encode a Go object to a YAML document func NewEncoder(w io.Writer) Encoder { if runtime.GoccyGoYaml { - return yaml.NewEncoder(w) + yamlEncoderOpts := []yaml.EncodeOption{} + // enable JSON style if the envvar is set + if os.Getenv(envvar.EnableGoccyGoYamlJSONStyle) == "true" { + yamlEncoderOpts = append(yamlEncoderOpts, yaml.JSON(), yaml.Flow(false)) + } + return yaml.NewEncoder(w, yamlEncoderOpts...) } return v2.NewEncoder(w)