1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

feat: add Pretty and PrettyToWriter

This commit is contained in:
dudaodong
2023-04-06 16:30:38 +08:00
parent 18f01ffd75
commit f23f18457e
5 changed files with 286 additions and 1 deletions

View File

@@ -5,7 +5,9 @@
package formatter
import (
"encoding/json"
"fmt"
"io"
"github.com/duke-git/lancet/v2/convertor"
"github.com/duke-git/lancet/v2/strutil"
@@ -44,3 +46,23 @@ func Comma[T constraints.Float | constraints.Integer | string](value T, symbol s
return ""
}
// Pretty data to JSON string.
// Play: todo
func Pretty(v any) (string, error) {
out, err := json.MarshalIndent(v, "", " ")
return string(out), err
}
// PrettyToWriter pretty encode data to writer.
// Play: todo
func PrettyToWriter(v any, out io.Writer) error {
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
return err
}
return nil
}