forked from francoispqt/gojay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_string.go
38 lines (33 loc) · 944 Bytes
/
encode_string.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
package gojay
// encodeString encodes a string to
func (enc *Encoder) encodeString(s string) ([]byte, error) {
enc.writeByte('"')
enc.writeString(s)
enc.writeByte('"')
return enc.buf, nil
}
// AddString adds a string to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddString(value string) error {
r, ok := enc.getPreviousRune()
if ok && r != '[' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeString(value)
enc.writeByte('"')
return nil
}
// AddStringKey adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddStringKey(key, value string) error {
// grow to avoid allocs (length of key/value + quotes)
r, ok := enc.getPreviousRune()
if ok && r != '{' && r != '[' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeString(key)
enc.write(objKeyStr)
enc.writeString(value)
enc.writeByte('"')
return nil
}