diff --git a/backend/bindings/debug_info.go b/backend/bindings/debug_info.go index b4244c3b..7c73aefb 100644 --- a/backend/bindings/debug_info.go +++ b/backend/bindings/debug_info.go @@ -3,7 +3,6 @@ package bindings import ( "archive/zip" "context" - "encoding/json" "fmt" "log/slog" "os" @@ -123,7 +122,7 @@ func addMetadata(writer *zip.Writer) error { SMLVersion: smlVersion, } - metadataBytes, err := json.MarshalIndent(metadata, "", " ") + metadataBytes, err := utils.JSONMarshal(metadata, 2) if err != nil { return errors.Wrap(err, "Failed to marshal metadata") } diff --git a/backend/bindings/ficsitcli/profiles.go b/backend/bindings/ficsitcli/profiles.go index 39fe9051..7fafe17c 100644 --- a/backend/bindings/ficsitcli/profiles.go +++ b/backend/bindings/ficsitcli/profiles.go @@ -202,7 +202,7 @@ func (f *FicsitCLI) ExportCurrentProfile() error { return errors.Wrapf(err, "Failed to export profile: %s", exportedProfile.Profile.Name) } - exportedProfileJSON, err := utils.JsonMarshal(exportedProfile, 2) + exportedProfileJSON, err := utils.JSONMarshal(exportedProfile, 2) if err != nil { l.Error("failed to marshal exported profile", slog.Any("error", err)) return errors.Wrapf(err, "Failed to export profile: %s", exportedProfile.Profile.Name) diff --git a/backend/settings/settings.go b/backend/settings/settings.go index 53020086..0c2fd1f9 100644 --- a/backend/settings/settings.go +++ b/backend/settings/settings.go @@ -114,7 +114,7 @@ func LoadSettings() error { } func SaveSettings() error { - settingsFile, err := json.MarshalIndent(Settings, "", " ") + settingsFile, err := utils.JSONMarshal(Settings, 2) if err != nil { return errors.Wrap(err, "failed to marshal settings") } diff --git a/backend/utils/json.go b/backend/utils/json.go new file mode 100644 index 00000000..cd4cee71 --- /dev/null +++ b/backend/utils/json.go @@ -0,0 +1,19 @@ +package utils + +import ( + "bytes" + "encoding/json" + "strings" +) + +func JSONMarshal(v any, indentSize int) ([]byte, error) { + buf := new(bytes.Buffer) + enc := json.NewEncoder(buf) + enc.SetEscapeHTML(false) + enc.SetIndent("", strings.Repeat(" ", indentSize)) + err := enc.Encode(v) + if err != nil { + return nil, err // nolint:wrapcheck + } + return buf.Bytes(), nil +}