MCPcopy Create free account
hub / github.com/francoispqt/gojay / Encode

Method Encode

encode_interface.go:11–49  ·  view source on GitHub ↗

Encode encodes a value to JSON. If Encode cannot find a way to encode the type to JSON it will return an InvalidMarshalError.

(v interface{})

Source from the content-addressed store, hash-verified

9// If Encode cannot find a way to encode the type to JSON
10// it will return an InvalidMarshalError.
11func (enc *Encoder) Encode(v interface{}) error {
12 if enc.isPooled == 1 {
13 panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
14 }
15 switch vt := v.(type) {
16 case string:
17 return enc.EncodeString(vt)
18 case bool:
19 return enc.EncodeBool(vt)
20 case MarshalerJSONArray:
21 return enc.EncodeArray(vt)
22 case MarshalerJSONObject:
23 return enc.EncodeObject(vt)
24 case int:
25 return enc.EncodeInt(vt)
26 case int64:
27 return enc.EncodeInt64(vt)
28 case int32:
29 return enc.EncodeInt(int(vt))
30 case int8:
31 return enc.EncodeInt(int(vt))
32 case uint64:
33 return enc.EncodeUint64(vt)
34 case uint32:
35 return enc.EncodeInt(int(vt))
36 case uint16:
37 return enc.EncodeInt(int(vt))
38 case uint8:
39 return enc.EncodeInt(int(vt))
40 case float64:
41 return enc.EncodeFloat(vt)
42 case float32:
43 return enc.EncodeFloat32(vt)
44 case *EmbeddedJSON:
45 return enc.EncodeEmbeddedJSON(vt)
46 default:
47 return InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
48 }
49}
50
51// AddInterface adds an interface{} to be encoded, must be used inside a slice or array encoding (does not encode a key)
52func (enc *Encoder) AddInterface(value interface{}) {

Callers 15

TestEncodingEmbeddedJSONFunction · 0.95
TestMessage_MarshalFunction · 0.95
TestMessage_MarshalFunction · 0.95
TestMessage_MarshalFunction · 0.95
TestMessage_MarshalFunction · 0.95
TestEncoderInt64Function · 0.80
TestEncoderInt32Function · 0.80
TestEncoderInt16Function · 0.80
TestEncoderInt8Function · 0.80
TestEncodeUint64Function · 0.80
TestEncoderFloat64Function · 0.80

Calls 12

EncodeStringMethod · 0.95
EncodeBoolMethod · 0.95
EncodeArrayMethod · 0.95
EncodeObjectMethod · 0.95
EncodeIntMethod · 0.95
EncodeInt64Method · 0.95
EncodeUint64Method · 0.95
EncodeFloatMethod · 0.95
EncodeFloat32Method · 0.95
EncodeEmbeddedJSONMethod · 0.95
InvalidMarshalErrorTypeAlias · 0.85

Tested by 15

TestEncodingEmbeddedJSONFunction · 0.76
TestMessage_MarshalFunction · 0.76
TestMessage_MarshalFunction · 0.76
TestMessage_MarshalFunction · 0.76
TestMessage_MarshalFunction · 0.76
TestEncoderInt64Function · 0.64
TestEncoderInt32Function · 0.64
TestEncoderInt16Function · 0.64
TestEncoderInt8Function · 0.64
TestEncodeUint64Function · 0.64
TestEncoderFloat64Function · 0.64