Skip to content

Commit

Permalink
perf: encoding/jsonutil: update WriteFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Dec 23, 2024
1 parent 850f6a6 commit 42ebc8d
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions encoding/jsonutil/jsonutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,14 @@ func UnmarshalFile(filename string, v any) ([]byte, error) {
}

func WriteFile(filename string, v any, prefix, indent string, perm fs.FileMode) error {
if b, err := MarshalSimple(v, prefix, indent); err != nil {
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
} else {
return os.WriteFile(filename, b, perm)
}
defer f.Close()
encr := json.NewEncoder(f)
encr.SetIndent(prefix, indent)
return encr.Encode(v)
}

func Equal(x, y io.Reader) (bool, error) {
Expand Down

0 comments on commit 42ebc8d

Please sign in to comment.