aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/tdewolff/minify/json
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/tdewolff/minify/json')
-rw-r--r--vendor/github.com/tdewolff/minify/json/json.go63
-rw-r--r--vendor/github.com/tdewolff/minify/json/json_test.go74
2 files changed, 137 insertions, 0 deletions
diff --git a/vendor/github.com/tdewolff/minify/json/json.go b/vendor/github.com/tdewolff/minify/json/json.go
new file mode 100644
index 0000000..ffe4b68
--- /dev/null
+++ b/vendor/github.com/tdewolff/minify/json/json.go
@@ -0,0 +1,63 @@
+// Package json minifies JSON following the specifications at http://json.org/.
+package json // import "github.com/tdewolff/minify/json"
+
+import (
+ "io"
+
+ "github.com/tdewolff/minify"
+ "github.com/tdewolff/parse/json"
+)
+
+var (
+ commaBytes = []byte(",")
+ colonBytes = []byte(":")
+)
+
+////////////////////////////////////////////////////////////////
+
+// DefaultMinifier is the default minifier.
+var DefaultMinifier = &Minifier{}
+
+// Minifier is a JSON minifier.
+type Minifier struct{}
+
+// 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 _, err := w.Write(text); err != nil {
+ return err
+ }
+ }
+}
diff --git a/vendor/github.com/tdewolff/minify/json/json_test.go b/vendor/github.com/tdewolff/minify/json/json_test.go
new file mode 100644
index 0000000..ce0e956
--- /dev/null
+++ b/vendor/github.com/tdewolff/minify/json/json_test.go
@@ -0,0 +1,74 @@
+package json // import "github.com/tdewolff/minify/json"
+
+import (
+ "bytes"
+ "fmt"
+ "os"
+ "regexp"
+ "testing"
+
+ "github.com/tdewolff/minify"
+ "github.com/tdewolff/test"
+)
+
+func TestJSON(t *testing.T) {
+ jsonTests := []struct {
+ json string
+ expected string
+ }{
+ {"{ \"a\": [1, 2] }", "{\"a\":[1,2]}"},
+ {"[{ \"a\": [{\"x\": null}, true] }]", "[{\"a\":[{\"x\":null},true]}]"},
+ {"{ \"a\": 1, \"b\": 2 }", "{\"a\":1,\"b\":2}"},
+ }
+
+ m := minify.New()
+ for _, tt := range jsonTests {
+ t.Run(tt.json, func(t *testing.T) {
+ r := bytes.NewBufferString(tt.json)
+ w := &bytes.Buffer{}
+ err := Minify(m, w, r, nil)
+ test.Minify(t, tt.json, err, w.String(), tt.expected)
+ })
+ }
+}
+
+func TestReaderErrors(t *testing.T) {
+ r := test.NewErrorReader(0)
+ w := &bytes.Buffer{}
+ m := minify.New()
+ err := Minify(m, w, r, nil)
+ test.T(t, err, test.ErrPlain, "return error at first read")
+}
+
+func TestWriterErrors(t *testing.T) {
+ errorTests := []struct {
+ json string
+ n []int
+ }{
+ //01 234 56 78
+ {`{"key":[100,200]}`, []int{0, 1, 2, 3, 4, 5, 7, 8}},
+ }
+
+ m := minify.New()
+ for _, tt := range errorTests {
+ for _, n := range tt.n {
+ t.Run(fmt.Sprint(tt.json, " ", tt.n), func(t *testing.T) {
+ r := bytes.NewBufferString(tt.json)
+ w := test.NewErrorWriter(n)
+ err := Minify(m, w, r, nil)
+ test.T(t, err, test.ErrPlain)
+ })
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////
+
+func ExampleMinify() {
+ m := minify.New()
+ m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), Minify)
+
+ if err := m.Minify("application/json", os.Stdout, os.Stdin); err != nil {
+ panic(err)
+ }
+}