Skip to content

Commit

Permalink
feat: enhance WriteToFileAsJSON with pretty-printing support (#783)
Browse files Browse the repository at this point in the history
* feat: indent json files for pretty printing

* feat: use ConvertToJSON function
  • Loading branch information
RoseSecurity authored Nov 15, 2024
1 parent 1368b54 commit 81f6db2
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/utils/json_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ func WriteToFileAsJSON(filePath string, data any, fileMode os.FileMode) error {
if err != nil {
return err
}
err = os.WriteFile(filePath, []byte(j), fileMode)

// Convert data to indented JSON
indentedJSON, err := json.MarshalIndent(json.RawMessage(j), "", " ")
if err != nil {
return err
}

err = os.WriteFile(filePath, indentedJSON, fileMode)
if err != nil {
return err
}
Expand All @@ -53,7 +60,7 @@ func WriteToFileAsJSON(filePath string, data any, fileMode os.FileMode) error {

// ConvertToJSON converts the provided value to a JSON-encoded string
func ConvertToJSON(data any) (string, error) {
var jc = jsoniter.Config{
jc := jsoniter.Config{
EscapeHTML: true,
ObjectFieldMustBeSimpleString: false,
SortMapKeys: true,
Expand All @@ -69,7 +76,7 @@ func ConvertToJSON(data any) (string, error) {

// ConvertToJSONFast converts the provided value to a JSON-encoded string using 'ConfigFastest' config and json.Marshal without indents
func ConvertToJSONFast(data any) (string, error) {
var jc = jsoniter.Config{
jc := jsoniter.Config{
EscapeHTML: false,
MarshalFloatWith6Digits: true,
ObjectFieldMustBeSimpleString: true,
Expand All @@ -86,7 +93,7 @@ func ConvertToJSONFast(data any) (string, error) {

// ConvertFromJSON converts the provided JSON-encoded string to Go data types
func ConvertFromJSON(jsonString string) (any, error) {
var jc = jsoniter.Config{
jc := jsoniter.Config{
EscapeHTML: false,
MarshalFloatWith6Digits: true,
ObjectFieldMustBeSimpleString: true,
Expand Down

0 comments on commit 81f6db2

Please sign in to comment.