Skip to content

Commit 1b4f5f9

Browse files
authored
Add Indent (#10)
1 parent 5796af6 commit 1b4f5f9

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

example_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,28 @@ func ExampleNumber() {
7575
// "z": "123456.00000000000000000000000000000000000000001"
7676
//}
7777
}
78+
79+
func ExampleIndent() {
80+
j := jsn.O{
81+
"x": 123456.00000000000000000000000000000000000000001,
82+
"y": map[string]any{
83+
"a": []int{1, 2, 3},
84+
"b": "ccc",
85+
},
86+
}
87+
88+
fmt.Printf("%s\n", jsn.Indent(j))
89+
90+
// Output:
91+
// {
92+
// "x": 123456,
93+
// "y": {
94+
// "a": [
95+
// 1,
96+
// 2,
97+
// 3
98+
// ],
99+
// "b": "ccc"
100+
// }
101+
// }
102+
}

jsn.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ func MustMarshal(obj any) []byte {
5353
return b
5454
}
5555

56+
// Indent is like [json.MarshalIndent] but with predefined params.
57+
// Panics if any error occurs.
58+
func Indent(obj any) string {
59+
b, err := json.MarshalIndent(obj, "", " ")
60+
if err != nil {
61+
panic(err)
62+
}
63+
return string(b)
64+
}
65+
5666
var errMore = errors.New("body must contain only one JSON entity")

0 commit comments

Comments
 (0)