-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsoncjson.go
173 lines (144 loc) · 2.61 KB
/
jsoncjson.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Package jsoncjson provides JSONC (JSON with comment) reader that
// removes all comments from the input.
//
// For example following input:
//
// { /* Comment. */
// "Hello": "World" // Comment.
// }
//
// Produces:
//
// {
// "Hello": "World"
// }
//
package jsoncjson
import (
"bytes"
"errors"
"io"
)
type jsoncTranslator struct {
r io.Reader
data []byte
end bool
cursor int
lastByte byte
lastToken token
}
// NewReader creates new reader that removes all comments from a jsonc
// input and returns pure json.
func NewReader(r io.Reader) io.Reader {
return &jsoncTranslator{
r: r,
data: make([]byte, bytes.MinRead),
cursor: bytes.MinRead,
lastToken: tokenOther,
}
}
// Read implements io.Reader interface.
func (t *jsoncTranslator) Read(jsonOut []byte) (n int, err error) {
for n = range jsonOut {
jsonOut[n], err = t.next()
if err != nil {
return n, err
}
}
return n + 1, nil
}
type token int8
const (
tokenString token = iota
tokenSingleComment
tokenMultiComment
tokenUnknownComment
tokenOther
tokenEscaping
)
func (t *jsoncTranslator) handleToken(curByte byte) (skip bool) {
switch t.lastToken {
case tokenString:
switch curByte {
case '"':
t.lastToken = tokenOther
case '\\':
t.lastToken = tokenEscaping
}
return false
case tokenEscaping:
t.lastToken = tokenString
return false
case tokenSingleComment:
if curByte == '\n' {
t.lastToken = tokenOther
}
return true
case tokenMultiComment:
if curByte == '/' && t.lastByte == '*' {
t.lastToken = tokenOther
}
return true
case tokenUnknownComment:
switch curByte {
case '/':
t.lastToken = tokenSingleComment
case '*':
t.lastToken = tokenMultiComment
}
return true
}
switch curByte {
case '"':
t.lastToken = tokenString
case '/':
t.lastToken = tokenUnknownComment
return true
}
return false
}
func (t *jsoncTranslator) refreshBuffer() (err error) {
var n int
n, err = t.r.Read(t.data)
if err != nil {
switch {
case errors.Is(err, io.EOF):
t.end = true
default:
return err
}
}
t.data = t.data[:n]
return nil
}
func (t *jsoncTranslator) next() (b byte, err error) {
b, err = t.nextRawByte()
if err != nil {
return b, err
}
var skip = t.handleToken(b)
if skip {
t.lastByte = b
return t.next()
}
t.lastByte = b
return b, nil
}
func (t *jsoncTranslator) nextRawByte() (b byte, err error) {
if t.cursor >= len(t.data) {
if t.end {
return 0, io.EOF
}
err = t.refreshBuffer()
if err != nil {
return 0, err
}
t.cursor = 0
if len(t.data) == 0 {
return 0, io.EOF
}
}
b = t.data[t.cursor]
t.cursor++
return b, nil
}