From ae42d61b50da534f9198a1955c054ac883be1d93 Mon Sep 17 00:00:00 2001 From: bounoable Date: Tue, 2 Apr 2024 14:14:49 +0200 Subject: [PATCH] feat(cli.go): replace json.Marshal with custom jsonMarshal function to prevent HTML escaping and maintain consistent indentation --- internal/cli/cli.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index e12033a..33b660f 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -149,7 +149,7 @@ func (app *App) Run() { app.kong.FatalIfErrorf(err, "failed to extract missing fields from source") } - if source, err = json.Marshal(sourceMap); err != nil { + if source, err = jsonMarshal(sourceMap); err != nil { app.kong.FatalIfErrorf(err, "failed to marshal source map") } } @@ -180,7 +180,7 @@ func (app *App) Run() { } dragoman.JSONMerge(originalOutMap, resultMap) - marshaled, err := json.MarshalIndent(originalOutMap, "", " ") + marshaled, err := jsonMarshal(originalOutMap) if err != nil { app.kong.FatalIfErrorf(err, "failed to marshal result map") } @@ -253,3 +253,12 @@ func readAll(r io.Reader) (out []byte, err error) { } } } + +func jsonMarshal(v any) ([]byte, error) { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) + enc.SetEscapeHTML(false) + enc.SetIndent("", " ") + err := enc.Encode(v) + return buf.Bytes(), err +}