Skip to content

Commit

Permalink
Add helper method for JSON marshaling with no HTML encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Dec 29, 2023
1 parent 47fd2dc commit 7370352
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 1 addition & 2 deletions backend/bindings/debug_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bindings
import (
"archive/zip"
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion backend/bindings/ficsitcli/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion backend/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
19 changes: 19 additions & 0 deletions backend/utils/json.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7370352

Please sign in to comment.