-
Notifications
You must be signed in to change notification settings - Fork 6
/
json.go
139 lines (122 loc) · 2.57 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
package signalr
import (
"bufio"
"bytes"
"io"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// Separator
const (
JSONSeparator = 0x1e
)
// JSONEncoding -
type JSONEncoding struct {
}
// NewJSONEncoding -
func NewJSONEncoding() *JSONEncoding {
return &JSONEncoding{}
}
// Decode -
func (e *JSONEncoding) Decode(data []byte) (interface{}, error) {
var typ Type
if err := json.Unmarshal(data, &typ); err != nil {
return nil, err
}
switch typ.Type {
case MessageTypeInvocation:
var msg Invocation
err := json.Unmarshal(data, &msg)
return msg, err
case MessageTypeStreamItem:
var msg StreamItem
err := json.Unmarshal(data, &msg)
return msg, err
case MessageTypeCompletion:
var msg Completion
err := json.Unmarshal(data, &msg)
return msg, err
case MessageTypeStreamInvocation:
var msg StreamInvocation
err := json.Unmarshal(data, &msg)
return msg, err
case MessageTypeCancelInvocation:
var msg CancelInvocation
err := json.Unmarshal(data, &msg)
return msg, err
case MessageTypePing:
var msg PingMessage
err := json.Unmarshal(data, &msg)
return msg, err
case MessageTypeCloseMessage:
var msg CloseMessage
err := json.Unmarshal(data, &msg)
return msg, err
default:
return nil, errors.Wrapf(ErrUnknownMessageType, "%d", typ.Type)
}
}
// Encode -
func (e *JSONEncoding) Encode(msg interface{}) ([]byte, error) {
data, err := json.Marshal(msg)
if err != nil {
return nil, err
}
return append(data, JSONSeparator), nil
}
// SplitJSON -
func SplitJSON(data []byte, atEOF bool) (advance int, token []byte, err error) {
for i := 0; i < len(data); i++ {
if data[i] == JSONSeparator {
return i + 1, data[:i], nil
}
}
if !atEOF {
return 0, nil, nil
}
return 0, data, bufio.ErrFinalToken
}
const (
chunkSize = 64 * 1024
)
// JSONReader -
type JSONReader struct {
reader io.Reader
buffer *bytes.Buffer
}
// NewJSONReader -
func NewJSONReader(r io.Reader) *JSONReader {
return &JSONReader{
reader: r,
buffer: bytes.NewBuffer(make([]byte, 0)),
}
}
// Scan -
func (r *JSONReader) Scan() error {
data := make([]byte, chunkSize)
for {
count, err := r.reader.Read(data)
switch err {
case io.EOF:
return nil
case nil:
if _, err := r.buffer.Write(data[:count]); err != nil {
return err
}
default:
return err
}
}
}
// Bytes -
func (r *JSONReader) Bytes() ([]byte, error) {
if r.buffer.Len() == 0 {
return nil, nil
}
data, err := r.buffer.ReadBytes(JSONSeparator)
if err != nil {
return nil, err
}
return data[:len(data)-1], nil
}