Skip to content

Commit 6452c4b

Browse files
authoredJul 8, 2024
chore: export MarshalIndent api in root (bytedance#668)
1 parent 765ecdb commit 6452c4b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
 

‎api.go

+7
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ func Marshal(val interface{}) ([]byte, error) {
157157
return ConfigDefault.Marshal(val)
158158
}
159159

160+
// MarshalIndent is like Marshal but applies Indent to format the output.
161+
// Each JSON element in the output will begin on a new line beginning with prefix
162+
// followed by one or more copies of indent according to the indentation nesting.
163+
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
164+
return ConfigDefault.MarshalIndent(v, prefix, indent)
165+
}
166+
160167
// MarshalString returns the JSON encoding string of v.
161168
func MarshalString(val interface{}) (string, error) {
162169
return ConfigDefault.MarshalToString(val)

‎api_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ func TestValid(t *testing.T) {
4949
require.Equal(t, tc.expected, Valid([]byte(tc.data)), tc.data)
5050
}
5151
}
52+
53+
54+
func TestIdent(t *testing.T) {
55+
foo := struct {
56+
Name string
57+
Age int
58+
}{
59+
Name: "sonic",
60+
Age: 20,
61+
}
62+
63+
out, err := MarshalIndent(&foo, "", " ")
64+
require.Nil(t, err)
65+
require.Equal(t, `{
66+
"Name": "sonic",
67+
"Age": 20
68+
}`, string(out))
69+
}

0 commit comments

Comments
 (0)