forked from francoispqt/gojay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
196 lines (189 loc) · 4.47 KB
/
encode.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
package gojay
// MarshalObject returns the JSON encoding of v.
//
// It takes a struct implementing Marshaler to a JSON slice of byte
// it returns a slice of bytes and an error.
// Example with an Marshaler:
// type TestStruct struct {
// id int
// }
// func (s *TestStruct) MarshalObject(enc *gojay.Encoder) {
// enc.AddIntKey("id", s.id)
// }
// func (s *TestStruct) IsNil() bool {
// return s == nil
// }
//
// func main() {
// test := &TestStruct{
// id: 123456,
// }
// b, _ := gojay.Marshal(test)
// fmt.Println(b) // {"id":123456}
// }
func MarshalObject(v MarshalerObject) ([]byte, error) {
enc := NewEncoder()
enc.grow(200)
enc.writeByte('{')
v.MarshalObject(enc)
enc.writeByte('}')
defer enc.addToPool()
return enc.buf, nil
}
// MarshalArray returns the JSON encoding of v.
//
// It takes an array or a slice implementing Marshaler to a JSON slice of byte
// it returns a slice of bytes and an error.
// Example with an Marshaler:
// type TestSlice []*TestStruct
//
// func (t TestSlice) MarshalArray(enc *Encoder) {
// for _, e := range t {
// enc.AddObject(e)
// }
// }
//
// func main() {
// test := &TestSlice{
// &TestStruct{123456},
// &TestStruct{7890},
// }
// b, _ := Marshal(test)
// fmt.Println(b) // [{"id":123456},{"id":7890}]
// }
func MarshalArray(v MarshalerArray) ([]byte, error) {
enc := NewEncoder()
enc.grow(200)
enc.writeByte('[')
v.(MarshalerArray).MarshalArray(enc)
enc.writeByte(']')
defer enc.addToPool()
return enc.buf, nil
}
// Marshal returns the JSON encoding of v.
//
// Marshal takes interface v and encodes it according to its type.
// Basic example with a string:
// b, err := gojay.Marshal("test")
// fmt.Println(b) // "test"
//
// If v implements Marshaler or Marshaler interface
// it will call the corresponding methods.
//
// If a struct, slice, or array is passed and does not implement these interfaces
// it will return a a non nil InvalidTypeError error.
// Example with an Marshaler:
// type TestStruct struct {
// id int
// }
// func (s *TestStruct) MarshalObject(enc *gojay.Encoder) {
// enc.AddIntKey("id", s.id)
// }
// func (s *TestStruct) IsNil() bool {
// return s == nil
// }
//
// func main() {
// test := &TestStruct{
// id: 123456,
// }
// b, _ := gojay.Marshal(test)
// fmt.Println(b) // {"id":123456}
// }
func Marshal(v interface{}) ([]byte, error) {
var b []byte
var err error = InvalidTypeError("Unknown type to Marshal")
switch vt := v.(type) {
case MarshalerObject:
enc := NewEncoder()
enc.writeByte('{')
vt.MarshalObject(enc)
enc.writeByte('}')
b = enc.buf
defer enc.addToPool()
return b, nil
case MarshalerArray:
enc := NewEncoder()
enc.writeByte('[')
vt.MarshalArray(enc)
enc.writeByte(']')
b = enc.buf
defer enc.addToPool()
return b, nil
case string:
enc := NewEncoder()
b, err = enc.encodeString(vt)
defer enc.addToPool()
case bool:
enc := NewEncoder()
err = enc.AddBool(vt)
b = enc.buf
defer enc.addToPool()
case int:
enc := NewEncoder()
b, err = enc.encodeInt(int64(vt))
defer enc.addToPool()
case int64:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeInt(vt)
case int32:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeInt(int64(vt))
case int16:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeInt(int64(vt))
case int8:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeInt(int64(vt))
case uint64:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeInt(int64(vt))
case uint32:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeInt(int64(vt))
case uint16:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeInt(int64(vt))
case uint8:
enc := NewEncoder()
b, err = enc.encodeInt(int64(vt))
defer enc.addToPool()
case float64:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeFloat(vt)
case float32:
enc := NewEncoder()
defer enc.addToPool()
return enc.encodeFloat(float64(vt))
}
return b, err
}
// MarshalerObject is the interface to implement for struct to be encoded
type MarshalerObject interface {
MarshalObject(enc *Encoder)
IsNil() bool
}
// MarshalerArray is the interface to implement
// for a slice or an array to be encoded
type MarshalerArray interface {
MarshalArray(enc *Encoder)
}
// An Encoder writes JSON values to an output stream.
type Encoder struct {
buf []byte
}
func (enc *Encoder) getPreviousRune() (byte, bool) {
last := len(enc.buf) - 1
if last < 0 {
return 0, false
}
return enc.buf[last], true
}