...
1
2
3
4
5 package json
6
7 import (
8 "bytes"
9 )
10
11
12
13 func Compact(dst *bytes.Buffer, src []byte) error {
14 return compact(dst, src, false)
15 }
16
17 func compact(dst *bytes.Buffer, src []byte, escape bool) error {
18 origLen := dst.Len()
19 scan := newScanner()
20 defer freeScanner(scan)
21 start := 0
22 for i, c := range src {
23 if escape && (c == '<' || c == '>' || c == '&') {
24 if start < i {
25 dst.Write(src[start:i])
26 }
27 dst.WriteString(`\u00`)
28 dst.WriteByte(hex[c>>4])
29 dst.WriteByte(hex[c&0xF])
30 start = i + 1
31 }
32
33 if escape && c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
34 if start < i {
35 dst.Write(src[start:i])
36 }
37 dst.WriteString(`\u202`)
38 dst.WriteByte(hex[src[i+2]&0xF])
39 start = i + 3
40 }
41 v := scan.step(scan, c)
42 if v >= scanSkipSpace {
43 if v == scanError {
44 break
45 }
46 if start < i {
47 dst.Write(src[start:i])
48 }
49 start = i + 1
50 }
51 }
52 if scan.eof() == scanError {
53 dst.Truncate(origLen)
54 return scan.err
55 }
56 if start < len(src) {
57 dst.Write(src[start:])
58 }
59 return nil
60 }
61
62 func newline(dst *bytes.Buffer, prefix, indent string, depth int) {
63 dst.WriteByte('\n')
64 dst.WriteString(prefix)
65 for i := 0; i < depth; i++ {
66 dst.WriteString(indent)
67 }
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81 func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
82 origLen := dst.Len()
83 scan := newScanner()
84 defer freeScanner(scan)
85 needIndent := false
86 depth := 0
87 for _, c := range src {
88 scan.bytes++
89 v := scan.step(scan, c)
90 if v == scanSkipSpace {
91 continue
92 }
93 if v == scanError {
94 break
95 }
96 if needIndent && v != scanEndObject && v != scanEndArray {
97 needIndent = false
98 depth++
99 newline(dst, prefix, indent, depth)
100 }
101
102
103
104 if v == scanContinue {
105 dst.WriteByte(c)
106 continue
107 }
108
109
110 switch c {
111 case '{', '[':
112
113 needIndent = true
114 dst.WriteByte(c)
115
116 case ',':
117 dst.WriteByte(c)
118 newline(dst, prefix, indent, depth)
119
120 case ':':
121 dst.WriteByte(c)
122 dst.WriteByte(' ')
123
124 case '}', ']':
125 if needIndent {
126
127 needIndent = false
128 } else {
129 depth--
130 newline(dst, prefix, indent, depth)
131 }
132 dst.WriteByte(c)
133
134 default:
135 dst.WriteByte(c)
136 }
137 }
138 if scan.eof() == scanError {
139 dst.Truncate(origLen)
140 return scan.err
141 }
142 return nil
143 }
144
View as plain text