Skip to content

Commit

Permalink
feat(cli.go): replace json.Marshal with custom jsonMarshal function t…
Browse files Browse the repository at this point in the history
…o prevent HTML escaping and maintain consistent indentation
  • Loading branch information
bounoable committed Apr 2, 2024
1 parent 482a21d commit ae42d61
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}

0 comments on commit ae42d61

Please sign in to comment.