-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_complex.go
More file actions
117 lines (108 loc) · 2.51 KB
/
handle_complex.go
File metadata and controls
117 lines (108 loc) · 2.51 KB
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
/*
* Copyright (c) 2022, arivum.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
package json2msgpackStreamer
func (t *JSON2MsgPackStreamer) handleArray() error {
var (
err error
numValues = 0
typePos, sizePos *blockBufPos
)
typePos = t.buf.getCurrentPos()
t.buf.appendByte(msgPackFlagArray16)
sizePos = t.buf.getCurrentPos()
for {
if t.nextByte, err = t.r.ReadByte(); err != nil {
return err
}
switch t.nextByte {
case ' ', '\n', '\r', ',', '\t':
// Do nothing
case '"':
if err = t.handleString(); err != nil {
return err
}
numValues++
case '{':
if err = t.handleStruct(); err != nil {
return err
}
numValues++
case '[':
if err = t.handleArray(); err != nil {
return err
}
numValues++
case ']':
switch {
case numValues < max4BitPlusOne:
t.buf.writeByteToOffset(msgPackFlagFixArray|byte(numValues), typePos)
case numValues < max16BitPlusOne:
t.buf.insertOnOffset(lenTo2Bytes(numValues), sizePos)
case numValues < max32BitPlusOne:
t.buf.writeByteToOffset(msgPackFlagArray32, typePos)
t.buf.insertOnOffset(lenTo4Bytes(numValues), sizePos)
}
return nil
default:
t.r.UnreadByte()
if err = t.handleAtomic(); err != nil {
return err
}
numValues++
}
}
}
func (t *JSON2MsgPackStreamer) handleStruct() error {
var (
err error
numKeys = 0
typePos, sizePos *blockBufPos
)
typePos = t.buf.getCurrentPos()
t.buf.appendByte(msgPackFlagMap16)
sizePos = t.buf.getCurrentPos()
for {
if t.nextByte, err = t.r.ReadByte(); err != nil {
return err
}
switch t.nextByte {
case ' ', '\n', '\r', ',', '\t':
// Do nothing
case ':':
// value start
numKeys++
case '"':
if err = t.handleString(); err != nil {
return err
}
case '{':
if err = t.handleStruct(); err != nil {
return err
}
case '[':
if err = t.handleArray(); err != nil {
return err
}
case '}':
switch {
case numKeys < max4BitPlusOne:
t.buf.writeByteToOffset(msgPackFlagFixMap|byte(numKeys), typePos)
case numKeys < max16BitPlusOne:
t.buf.insertOnOffset(lenTo2Bytes(numKeys), sizePos)
case numKeys < max32BitPlusOne:
t.buf.writeByteToOffset(msgPackFlagMap32, typePos)
t.buf.insertOnOffset(lenTo4Bytes(numKeys), sizePos)
}
return nil
default:
t.r.UnreadByte()
if err = t.handleAtomic(); err != nil {
return err
}
}
}
}