diff --git a/example_test.go b/example_test.go index 4831f67..ac479e1 100644 --- a/example_test.go +++ b/example_test.go @@ -75,3 +75,28 @@ func ExampleNumber() { // "z": "123456.00000000000000000000000000000000000000001" //} } + +func ExampleIndent() { + j := jsn.O{ + "x": 123456.00000000000000000000000000000000000000001, + "y": map[string]any{ + "a": []int{1, 2, 3}, + "b": "ccc", + }, + } + + fmt.Printf("%s\n", jsn.Indent(j)) + + // Output: + // { + // "x": 123456, + // "y": { + // "a": [ + // 1, + // 2, + // 3 + // ], + // "b": "ccc" + // } + // } +} diff --git a/jsn.go b/jsn.go index cf2fdfa..e1b4f32 100644 --- a/jsn.go +++ b/jsn.go @@ -53,4 +53,14 @@ func MustMarshal(obj any) []byte { return b } +// Indent is like [json.MarshalIndent] but with predefined params. +// Panics if any error occurs. +func Indent(obj any) string { + b, err := json.MarshalIndent(obj, "", " ") + if err != nil { + panic(err) + } + return string(b) +} + var errMore = errors.New("body must contain only one JSON entity")