forked from zlyuancn/zjve2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
49 lines (39 loc) · 1.17 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
-------------------------------------------------
Author : Zhang Fan
date: 2020/1/14
Description :
-------------------------------------------------
*/
package zjve2
import (
"bytes"
"encoding/json"
"github.com/json-iterator/go"
)
var Json = jsoniter.ConfigCompatibleWithStandardLibrary
// 默认换行间隔
var DefaultFormatIndent = " "
// 将json文本格式化
func JsonFormat(s, indent string) (string, error) {
return JsonFormatBytes([]byte(s), indent)
}
// 将json字节数组格式化
func JsonFormatBytes(bs []byte, indent string) (string, error) {
if indent == "" {
indent = DefaultFormatIndent
}
var out bytes.Buffer
// jsoniter当前版本没有Indent
err := json.Indent(&out, bs, "", indent)
return out.String(), err
}
// 将对象格式化为json字符串
func JsonFormatObj(v interface{}, indent string) (string, error) {
if indent == "" {
indent = DefaultFormatIndent
}
// 现在还是使用官方的format, jsoniter当前版本展示方式不是很友好, 或许它以后会解决这个问题?
bs, err := json.MarshalIndent(v, "", indent)
return string(bs), err
}