From 0a0e7cf90c20d1492db7328a9eca6cc41e48a148 Mon Sep 17 00:00:00 2001 From: Deathstroke <776022703@qq.com> Date: Sat, 11 May 2024 14:30:48 +0800 Subject: [PATCH] Explicitly convert inMediaType json to outMediaType yaml Signed-off-by: Deathstroke <776022703@qq.com> --- pkg/encoding/encoding.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/encoding/encoding.go b/pkg/encoding/encoding.go index 14d8606..592709f 100644 --- a/pkg/encoding/encoding.go +++ b/pkg/encoding/encoding.go @@ -95,6 +95,15 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i if outMediaType == JsonMediaType { encoded = append(encoded, '\n') } + } else if inMediaType == JsonMediaType && outMediaType == YamlMediaType { + val := map[string]interface{}{} + if err := json.Unmarshal(in, &val); err != nil { + return nil, fmt.Errorf("error decoding from %s: %s", inMediaType, err) + } + encoded, err = yaml.Marshal(val) + if err != nil { + return nil, fmt.Errorf("error encoding from %s: %s", outMediaType, err) + } } else { inCodec, err := newCodec(codecs, typeMeta, inMediaType) if err != nil {