-
Notifications
You must be signed in to change notification settings - Fork 114
/
decode_embedded_json.go
85 lines (81 loc) · 2.12 KB
/
decode_embedded_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
package gojay
// EmbeddedJSON is a raw encoded JSON value.
// It can be used to delay JSON decoding or precompute a JSON encoding.
type EmbeddedJSON []byte
func (dec *Decoder) decodeEmbeddedJSON(ej *EmbeddedJSON) error {
var err error
if ej == nil {
return InvalidUnmarshalError("Invalid nil pointer given")
}
var beginOfEmbeddedJSON int
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
// is null
case 'n':
beginOfEmbeddedJSON = dec.cursor
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
case 't':
beginOfEmbeddedJSON = dec.cursor
dec.cursor++
err := dec.assertTrue()
if err != nil {
return err
}
// is false
case 'f':
beginOfEmbeddedJSON = dec.cursor
dec.cursor++
err := dec.assertFalse()
if err != nil {
return err
}
// is an object
case '{':
beginOfEmbeddedJSON = dec.cursor
dec.cursor = dec.cursor + 1
dec.cursor, err = dec.skipObject()
// is string
case '"':
beginOfEmbeddedJSON = dec.cursor
dec.cursor = dec.cursor + 1
err = dec.skipString() // why no new dec.cursor in result?
// is array
case '[':
beginOfEmbeddedJSON = dec.cursor
dec.cursor = dec.cursor + 1
dec.cursor, err = dec.skipArray()
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
beginOfEmbeddedJSON = dec.cursor
dec.cursor, err = dec.skipNumber()
}
break
}
if err == nil {
if dec.cursor-1 >= beginOfEmbeddedJSON {
*ej = append(*ej, dec.data[beginOfEmbeddedJSON:dec.cursor]...)
}
dec.called |= 1
}
return err
}
// AddEmbeddedJSON adds an EmbeddedsJSON to the value pointed by v.
// It can be used to delay JSON decoding or precompute a JSON encoding.
func (dec *Decoder) AddEmbeddedJSON(v *EmbeddedJSON) error {
return dec.EmbeddedJSON(v)
}
// EmbeddedJSON adds an EmbeddedsJSON to the value pointed by v.
// It can be used to delay JSON decoding or precompute a JSON encoding.
func (dec *Decoder) EmbeddedJSON(v *EmbeddedJSON) error {
err := dec.decodeEmbeddedJSON(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}