Skip to content

Commit 155947e

Browse files
committed
Don't HTML-sanitize JSON output
By default, the `json` package HTML-sanitizes strings during marshalling. This is not useful for this application and resulted in the JSON output containing gibberish.
1 parent a58dfe9 commit 155947e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

internal/result/result.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23+
"io"
2324
"text/template"
2425

2526
"github.com/arduino/arduino-lint/internal/configuration"
@@ -216,12 +217,18 @@ func (results Type) JSONReport() string {
216217
}
217218

218219
func (results Type) jsonReportRaw() []byte {
219-
reportJSON, err := json.MarshalIndent(results, "", " ")
220+
var marshalledReportBuffer bytes.Buffer
221+
jsonEncoder := json.NewEncoder(io.Writer(&marshalledReportBuffer))
222+
// By default, the json package HTML-sanitizes strings during marshalling (https://golang.org/pkg/encoding/json/#Marshal)
223+
// This means that the simple json.MarshalIndent() approach would result in the report containing gibberish.
224+
jsonEncoder.SetEscapeHTML(false)
225+
jsonEncoder.SetIndent("", " ")
226+
err := jsonEncoder.Encode(results)
220227
if err != nil {
221228
panic(fmt.Sprintf("Error while formatting rules report: %v", err))
222229
}
223230

224-
return reportJSON
231+
return marshalledReportBuffer.Bytes()
225232
}
226233

227234
// WriteReport writes a report for all projects to the specified file.

0 commit comments

Comments
 (0)