-
Notifications
You must be signed in to change notification settings - Fork 6
/
json.go
49 lines (38 loc) · 1.11 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
package lib
import (
"encoding/json"
"github.com/tidwall/pretty"
)
type JSONFormater interface {
TabIndent(string) string
TwoSpacesIndent(string) string
FourSpacesIndent(string) string
Minify(string) string
IsJSON(string) bool
}
var (
tab = &pretty.Options{Indent: " ", Width: 80, Prefix: "", SortKeys: false}
twoSpaces = &pretty.Options{Indent: " ", Width: 80, Prefix: "", SortKeys: false}
fourSpaces = &pretty.Options{Indent: " ", Width: 80, Prefix: "", SortKeys: false}
)
type JSONFormat struct {
}
func (j *JSONFormat) TabIndent(s string) string {
return string(pretty.PrettyOptions([]byte(s), tab))
}
func (j *JSONFormat) TwoSpacesIndent(s string) string {
return string(pretty.PrettyOptions([]byte(s), twoSpaces))
}
func (j *JSONFormat) FourSpacesIndent(s string) string {
return string(pretty.PrettyOptions([]byte(s), fourSpaces))
}
func (j *JSONFormat) Minify(s string) string {
return string(pretty.Ugly([]byte(s)))
}
func (j *JSONFormat) IsJSON(s string) bool {
var js interface{}
return json.Unmarshal([]byte(s), &js) == nil
}
func NewJSONFormat() JSONFormater {
return &JSONFormat{}
}