-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson.go
164 lines (134 loc) · 2.78 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
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
package mendoza
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
type jsonWriter struct {
result []byte
err error
}
func (w *jsonWriter) WriteUint8(v uint8) error {
return w.WriteValue(v)
}
func (w *jsonWriter) WriteUint(v int) error {
return w.WriteValue(v)
}
func (w *jsonWriter) WriteString(v string) error {
return w.WriteValue(v)
}
func (w *jsonWriter) WriteValue(v interface{}) error {
w.next()
b, err := json.Marshal(v)
if err != nil {
return err
}
w.result = append(w.result, b...)
return nil
}
func (w *jsonWriter) next() {
if len(w.result) == 0 {
w.result = append(w.result, '[')
} else {
w.result = append(w.result, ',')
}
}
func (w *jsonWriter) finalize() []byte {
if len(w.result) == 0 {
return []byte{'[', ']'}
}
w.result = append(w.result, ']')
return w.result
}
type jsonReader struct {
dec *json.Decoder
}
func (r *jsonReader) tryEof() error {
if !r.dec.More() {
t, err := r.dec.Token()
if err != nil {
return err
}
if t != json.Delim(']') {
return fmt.Errorf("expected ] at end")
}
return io.EOF
}
return nil
}
func (r *jsonReader) ReadUint8() (uint8, error) {
return ReadUint8FromValueReader(r)
}
func (r *jsonReader) ReadUint() (int, error) {
return ReadUintFromValueReader(r)
}
func (r *jsonReader) ReadString() (string, error) {
return ReadStringFromValueReader(r)
}
func (r *jsonReader) ReadValue() (interface{}, error) {
err := r.tryEof()
if err != nil {
return nil, err
}
var val interface{}
err = r.dec.Decode(&val)
if err != nil {
return nil, err
}
return val, nil
}
func (r *jsonReader) expectArray() error {
t, err := r.dec.Token()
if err != nil {
return err
}
if t != json.Delim('[') {
return fmt.Errorf("expected array")
}
return nil
}
type jsonValueReader struct {
data []interface{}
idx int
}
func (r *jsonValueReader) ReadUint8() (uint8, error) {
return ReadUint8FromValueReader(r)
}
func (r *jsonValueReader) ReadUint() (int, error) {
return ReadUintFromValueReader(r)
}
func (r *jsonValueReader) ReadString() (string, error) {
return ReadStringFromValueReader(r)
}
func (r *jsonValueReader) ReadValue() (interface{}, error) {
if r.idx >= len(r.data) {
return nil, io.EOF
}
idx := r.idx
r.idx++
return r.data[idx], nil
}
func (patch Patch) MarshalJSON() ([]byte, error) {
w := jsonWriter{}
err := patch.WriteTo(&w)
if err != nil {
return nil, err
}
return w.finalize(), nil
}
func (patch *Patch) UnmarshalJSON(data []byte) error {
r := jsonReader{
dec: json.NewDecoder(bytes.NewReader(data)),
}
err := r.expectArray()
if err != nil {
return err
}
return patch.ReadFrom(&r)
}
// DecodeJSON decodes a patch from an []interface{} as parsed by encoding/json.
func (patch *Patch) DecodeJSON(data []interface{}) error {
r := jsonValueReader{data: data}
return patch.ReadFrom(&r)
}