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{})
| 9 | // If Encode cannot find a way to encode the type to JSON |
| 10 | // it will return an InvalidMarshalError. |
| 11 | func (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) |
| 52 | func (enc *Encoder) AddInterface(value interface{}) { |