-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
json.go
76 lines (64 loc) · 1.87 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Package json minifies JSON following the specifications at http://json.org/.
package json
import (
"io"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/parse/v2/json"
)
var (
commaBytes = []byte(",")
colonBytes = []byte(":")
zeroBytes = []byte("0")
minusZeroBytes = []byte("-0")
)
////////////////////////////////////////////////////////////////
// DefaultMinifier is the default minifier.
var DefaultMinifier = &Minifier{}
// Minifier is a JSON minifier.
type Minifier struct {
Precision int // number of significant digits
}
// Minify minifies JSON data, it reads from r and writes to w.
func Minify(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error {
return DefaultMinifier.Minify(m, w, r, params)
}
// Minify minifies JSON data, it reads from r and writes to w.
func (o *Minifier) Minify(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
skipComma := true
p := json.NewParser(r)
defer p.Restore()
for {
state := p.State()
gt, text := p.Next()
if gt == json.ErrorGrammar {
if p.Err() != io.EOF {
return p.Err()
}
return nil
}
if !skipComma && gt != json.EndObjectGrammar && gt != json.EndArrayGrammar {
if state == json.ObjectKeyState || state == json.ArrayState {
if _, err := w.Write(commaBytes); err != nil {
return err
}
} else if state == json.ObjectValueState {
if _, err := w.Write(colonBytes); err != nil {
return err
}
}
}
skipComma = gt == json.StartObjectGrammar || gt == json.StartArrayGrammar
if 0 < len(text) && ('0' <= text[0] && text[0] <= '9' || text[0] == '-') {
text = minify.Number(text, o.Precision)
if text[0] == '.' {
w.Write(zeroBytes)
} else if 1 < len(text) && text[0] == '-' && text[1] == '.' {
text = text[1:]
w.Write(minusZeroBytes)
}
}
if _, err := w.Write(text); err != nil {
return err
}
}
}