Skip to content

Commit

Permalink
Add Indent (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Nov 27, 2023
1 parent 5796af6 commit 1b4f5f9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
// }
// }
}
10 changes: 10 additions & 0 deletions jsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 1b4f5f9

Please sign in to comment.