-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvjson.go
414 lines (331 loc) · 8.8 KB
/
vjson.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Package vjson is a small, minimal alternative to encoding/json which has no dependencies
// and works with Tingyo. The Marshal and Unmarshal methods work like encoding/json, but
// structs are not supported, only primitives plus map[string]interface{} and []interface{}.
package vjson
import (
"bytes"
"errors"
"fmt"
"io"
"strconv"
)
// NOTE: various bits have been borrowed from encoding/json
// bool, for JSON booleans
// float64, for JSON numbers
// string, for JSON strings
// []interface{}, for JSON arrays
// map[string]interface{}, for JSON objects
// nil for JSON null
// RawMessage - just use Marshaler
// if someone asks to ask into an int type that should still work
func marshal(v interface{}) ([]byte, error) {
var buf bytes.Buffer
err := marshalTo(&buf, v)
return buf.Bytes(), err
}
func marshalTo(w io.Writer, vin interface{}) (err error) {
bb := make([]byte, 0, 64) // hopefully stack alloc
// TODO: check for Marshaler
// TODO: check for MarshalerTo
if vin == nil {
_, err = w.Write([]byte("null"))
return err
}
switch v := vin.(type) {
case bool:
bb = strconv.AppendBool(bb, v)
case float64:
return float64Encoder(w, v)
case float32:
return float32Encoder(w, float64(v))
case int:
bb = strconv.AppendInt(bb, int64(v), 10)
case int8:
bb = strconv.AppendInt(bb, int64(v), 10)
case int16:
bb = strconv.AppendInt(bb, int64(v), 10)
case int32:
bb = strconv.AppendInt(bb, int64(v), 10)
case int64:
bb = strconv.AppendInt(bb, v, 10)
case uint:
bb = strconv.AppendUint(bb, uint64(v), 10)
case uint8:
bb = strconv.AppendUint(bb, uint64(v), 10)
case uint16:
bb = strconv.AppendUint(bb, uint64(v), 10)
case uint32:
bb = strconv.AppendUint(bb, uint64(v), 10)
case uint64:
bb = strconv.AppendUint(bb, v, 10)
case string:
return encodeString(w, v, false)
// case []byte: // TODO: this is wrong - byte slice should get base64 encoded
// return encodeStringBytes(w, v, false)
case []interface{}:
if v == nil {
bb = append(bb, `null`...)
break
}
w.Write([]byte(`[`))
first := true
for i := range v {
if !first {
w.Write([]byte(`,`))
}
first = false
err := marshalTo(w, v[i])
if err != nil {
return err
}
}
_, err := w.Write([]byte(`]`))
return err
case map[string]interface{}:
if v == nil {
bb = append(bb, `null`...)
break
}
w.Write([]byte(`{`))
first := true
for k, el := range v {
if !first {
w.Write([]byte(`,`))
}
first = false
encodeString(w, k, false)
w.Write([]byte(`:`))
err := marshalTo(w, el)
if err != nil {
return err
}
}
_, err := w.Write([]byte(`}`))
return err
// case interface{}: // needed?
// TODO: pointer cases
default:
return fmt.Errorf("vjson.marshalTo error unknown type: %T", vin)
}
if len(bb) == 0 {
panic("unexpected zero length buffer") // should never happen
}
_, err = w.Write(bb)
return err
}
func unmarshal(data []byte, v interface{}) error {
return unmarshalFrom(bytes.NewReader(data), v)
}
func unmarshalFrom(r unreader, vin interface{}) error {
// read the next token, whatever it is
tok, err := readToken(r)
if err != nil {
return err
}
return unmarshalNext(r, tok, vin)
}
func unmarshalNext(r unreader, tok Token, vin interface{}) error {
tokDelim, _ := tok.(Delim)
// and type switch to determine how to handle it
switch v := vin.(type) {
case *interface{}:
if tok == nil {
*v = nil
} else {
return fmt.Errorf("vjson.unmarshalNext unable to scan %#v into %T", tok, v)
}
case *bool:
if tokv, ok := tok.(bool); ok {
*v = tokv
} else {
return fmt.Errorf("vjson.unmarshalNext unable to scan %#v into %T", tok, v)
}
case *float64:
if tokv, ok := tok.(Number); ok {
f, err := tokv.Float64()
if err != nil {
return err
}
*v = f
} else {
return fmt.Errorf("vjson.unmarshalNext unable to scan %#v into %T", tok, v)
}
// case *float32:
// case *int:
// case *int8:
// case *int16:
// case *int32:
// case *int64:
// case *uint:
// case *uint8:
// case *uint16:
// case *uint32:
// case *uint64:
case *string:
if tokv, ok := tok.(string); ok {
*v = tokv
} else {
return fmt.Errorf("vjson.unmarshalNext unable to scan %#v into %T", tok, v)
}
// case *[]byte: // hm, should be base64 encoded
// if tokv, ok := tok.(string); ok {
// *v = []byte(tokv)
// } else {
// return fmt.Errorf("vjson.unmarshalNext unable to scan %#v into %T", tok, v)
// }
case *[]interface{}:
// make sure we have an array start
if tokDelim != Delim('[') {
return fmt.Errorf("vjson.unmarshalNext unable to scan %#v into %T", tok, v)
}
sliceV := make([]interface{}, 0, 4)
for {
nextTok, err := readToken(r)
if err != nil {
return err
}
nextTokDelim, _ := nextTok.(Delim)
if nextTokDelim == Delim(']') {
break // end array
}
elV := newDefaultForToken(nextTok)
err = unmarshalNext(r, nextTok, elV)
if err != nil {
return err
}
sliceV = append(sliceV, deref(elV))
}
*v = sliceV
case *map[string]interface{}:
// make sure we have an object start
if tokDelim != Delim('{') {
return fmt.Errorf("vjson.unmarshalNext unable to scan %#v into %T", tok, v)
}
mapV := make(map[string]interface{}, 4)
for {
// read object key (must be string)
keyTok, err := readToken(r)
if err != nil {
return err
}
keyTokDelim, _ := keyTok.(Delim)
if keyTokDelim == Delim('}') {
break // end object
}
nextTok, err := readToken(r)
if err != nil {
return err
}
elV := newDefaultForToken(nextTok)
err = unmarshalNext(r, nextTok, elV)
if err != nil {
return err
}
keyTokStr, ok := keyTok.(string)
if !ok {
return fmt.Errorf("unexpected non-string object key token: %#v", keyTok)
}
mapV[keyTokStr] = deref(elV)
}
*v = mapV
default:
return fmt.Errorf("vjson.unmarshalNext error unknown type: %T", vin)
}
return nil
}
// newDefaultForToken will return a pointer to the appropriate type based on a JSON token.
// Used when scanning into an interface{} and we need to infer the Go type from the JSON input.
// A nil Token will return nil.
func newDefaultForToken(tok Token) interface{} {
if tok == nil {
return new(interface{})
}
switch tok.(type) {
case bool:
return new(bool)
case Number:
return new(float64)
case float64:
return new(float64)
case string:
return new(string)
}
tokDelim, _ := tok.(Delim)
if tokDelim == Delim('[') {
return new([]interface{})
} else if tokDelim == Delim('{') {
return new(map[string]interface{})
}
panic(fmt.Errorf("newDefaultForToken unexpected token %v (type=%T)", tok, tok))
}
// deref will strip the pointer off of the value returned by newDefaultForToken
func deref(vin interface{}) interface{} {
// if vin == nil {
// return nil
// }
switch v := vin.(type) {
case *interface{}:
if *v == nil {
return nil
} else {
panic(fmt.Errorf("deref: *interface{} should have been nil but got: %+v", *v))
}
case *bool:
return *v
case *string:
return *v
case *float64:
return *v
case *[]interface{}:
return *v
case *map[string]interface{}:
return *v
}
panic(fmt.Errorf("vjson.deref got unknown type %T", vin))
}
// unreader is implemented by bytes.Reader and bytes.Buffer
type unreader interface {
Read(p []byte) (n int, err error)
// ReadBytes(delim byte) (line []byte, err error)
ReadByte() (byte, error)
UnreadByte() error
}
// NOTE: for writing io.Writer works, but for reading io.Reader does NOT work because
// not all JSON data types have a termination character (e.g. you cannot tell when you've
// reached the end of a number without reading past it). One solution could be to define
// an interface with the methods we need from bytes.Reader, minimally Read() and UnreadByte()
// type MarshalerTo interface {
// MarshalJSONTo(w io.Writer) error
// }
// Marshaler is the interface implemented by types that can marshal themselves into valid JSON.
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
// type UnmarshalerFrom interface {
// UnmarshalJSONFrom(r io.Reader) error
// }
// Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves.
type Unmarshaler interface {
UnmarshalJSON([]byte) error
}
// RawMessage is a raw encoded JSON value.
// It implements Marshaler and Unmarshaler and can
// be used to delay JSON decoding or precompute a JSON encoding.
type RawMessage []byte
// MarshalJSON returns m as the JSON encoding of m.
func (m RawMessage) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return m, nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *RawMessage) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("vjson.RawMessage: UnmarshalJSON on nil pointer")
}
*m = append((*m)[0:0], data...)
return nil
}
var _ Marshaler = (*RawMessage)(nil)
var _ Unmarshaler = (*RawMessage)(nil)