Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: export MarshalIndent api in root #668

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ func Marshal(val interface{}) ([]byte, error) {
return ConfigDefault.Marshal(val)
}

// MarshalIndent is like Marshal but applies Indent to format the output.
// Each JSON element in the output will begin on a new line beginning with prefix
// followed by one or more copies of indent according to the indentation nesting.
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
return ConfigDefault.MarshalIndent(v, prefix, indent)
}

// MarshalString returns the JSON encoding string of v.
func MarshalString(val interface{}) (string, error) {
return ConfigDefault.MarshalToString(val)
Expand Down
18 changes: 18 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,21 @@ func TestValid(t *testing.T) {
require.Equal(t, tc.expected, Valid([]byte(tc.data)), tc.data)
}
}


func TestIdent(t *testing.T) {
foo := struct {
Name string
Age int
}{
Name: "sonic",
Age: 20,
}

out, err := MarshalIndent(&foo, "", " ")
require.Nil(t, err)
require.Equal(t, `{
"Name": "sonic",
"Age": 20
}`, string(out))
}
Loading