summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/francoispqt/gojay/encode_interface.go
blob: c4692e5fceae49f227b4b4d05b2bffd0c3c627b5 (plain) (blame)
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
package gojay

import (
	"fmt"
)

// Encode encodes a value to JSON.
//
// If Encode cannot find a way to encode the type to JSON
// it will return an InvalidMarshalError.
func (enc *Encoder) Encode(v interface{}) error {
	if enc.isPooled == 1 {
		panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
	}
	switch vt := v.(type) {
	case string:
		return enc.EncodeString(vt)
	case bool:
		return enc.EncodeBool(vt)
	case MarshalerJSONArray:
		return enc.EncodeArray(vt)
	case MarshalerJSONObject:
		return enc.EncodeObject(vt)
	case int:
		return enc.EncodeInt(vt)
	case int64:
		return enc.EncodeInt64(vt)
	case int32:
		return enc.EncodeInt(int(vt))
	case int8:
		return enc.EncodeInt(int(vt))
	case uint64:
		return enc.EncodeUint64(vt)
	case uint32:
		return enc.EncodeInt(int(vt))
	case uint16:
		return enc.EncodeInt(int(vt))
	case uint8:
		return enc.EncodeInt(int(vt))
	case float64:
		return enc.EncodeFloat(vt)
	case float32:
		return enc.EncodeFloat32(vt)
	case *EmbeddedJSON:
		return enc.EncodeEmbeddedJSON(vt)
	default:
		return InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
	}
}

// AddInterface adds an interface{} to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddInterface(value interface{}) {
	switch vt := value.(type) {
	case string:
		enc.AddString(vt)
	case bool:
		enc.AddBool(vt)
	case MarshalerJSONArray:
		enc.AddArray(vt)
	case MarshalerJSONObject:
		enc.AddObject(vt)
	case int:
		enc.AddInt(vt)
	case int64:
		enc.AddInt(int(vt))
	case int32:
		enc.AddInt(int(vt))
	case int8:
		enc.AddInt(int(vt))
	case uint64:
		enc.AddUint64(vt)
	case uint32:
		enc.AddInt(int(vt))
	case uint16:
		enc.AddInt(int(vt))
	case uint8:
		enc.AddInt(int(vt))
	case float64:
		enc.AddFloat(vt)
	case float32:
		enc.AddFloat32(vt)
	default:
		if vt != nil {
			enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
			return
		}
		return
	}
}

// AddInterfaceKey adds an interface{} to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddInterfaceKey(key string, value interface{}) {
	switch vt := value.(type) {
	case string:
		enc.AddStringKey(key, vt)
	case bool:
		enc.AddBoolKey(key, vt)
	case MarshalerJSONArray:
		enc.AddArrayKey(key, vt)
	case MarshalerJSONObject:
		enc.AddObjectKey(key, vt)
	case int:
		enc.AddIntKey(key, vt)
	case int64:
		enc.AddIntKey(key, int(vt))
	case int32:
		enc.AddIntKey(key, int(vt))
	case int16:
		enc.AddIntKey(key, int(vt))
	case int8:
		enc.AddIntKey(key, int(vt))
	case uint64:
		enc.AddIntKey(key, int(vt))
	case uint32:
		enc.AddIntKey(key, int(vt))
	case uint16:
		enc.AddIntKey(key, int(vt))
	case uint8:
		enc.AddIntKey(key, int(vt))
	case float64:
		enc.AddFloatKey(key, vt)
	case float32:
		enc.AddFloat32Key(key, vt)
	default:
		if vt != nil {
			enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
			return
		}
		return
	}
}

// AddInterfaceKeyOmitEmpty adds an interface{} to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddInterfaceKeyOmitEmpty(key string, v interface{}) {
	switch vt := v.(type) {
	case string:
		enc.AddStringKeyOmitEmpty(key, vt)
	case bool:
		enc.AddBoolKeyOmitEmpty(key, vt)
	case MarshalerJSONArray:
		enc.AddArrayKeyOmitEmpty(key, vt)
	case MarshalerJSONObject:
		enc.AddObjectKeyOmitEmpty(key, vt)
	case int:
		enc.AddIntKeyOmitEmpty(key, vt)
	case int64:
		enc.AddIntKeyOmitEmpty(key, int(vt))
	case int32:
		enc.AddIntKeyOmitEmpty(key, int(vt))
	case int16:
		enc.AddIntKeyOmitEmpty(key, int(vt))
	case int8:
		enc.AddIntKeyOmitEmpty(key, int(vt))
	case uint64:
		enc.AddIntKeyOmitEmpty(key, int(vt))
	case uint32:
		enc.AddIntKeyOmitEmpty(key, int(vt))
	case uint16:
		enc.AddIntKeyOmitEmpty(key, int(vt))
	case uint8:
		enc.AddIntKeyOmitEmpty(key, int(vt))
	case float64:
		enc.AddFloatKeyOmitEmpty(key, vt)
	case float32:
		enc.AddFloat32KeyOmitEmpty(key, vt)
	default:
		if vt != nil {
			enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
			return
		}
		return
	}
}