func (e Encoder) EncodeArray(n int, f func(Encoder) error) (err error) {
	if e.key {
		if e.key, err = false, e.Emitter.EmitMapValue(); err != nil {
			return
		}
	}
	if err = e.Emitter.EmitArrayBegin(n); err != nil {
		return
	}
encodeArray:
	for i := 0; n < 0 || i < n; i++ {
		if i != 0 {
			if e.Emitter.EmitArrayNext(); err != nil {
				return
			}
		}
		switch err = f(e); err {
		case nil:
		case End:
			break encodeArray
		default:
			return
		}
	}
	return e.Emitter.EmitArrayEnd()
}
EncodeArray provides the implementation of the array encoding algorithm,
where n is the number of elements in the array, and f a function called to
encode each element.

The n argument can be set to a negative value to indicate that the program
doesn't know how many elements it will output to the array. Be mindful that
not all emitters support encoding arrays of unknown lengths.

The f function is called to encode each element of the array.
package objconv
import (
	"encoding"
	"fmt"
	"io"
	"reflect"
	"time"
	"unsafe"
)
type Encoder struct {
	Emitter     Emitter  
	SortMapKeys bool     
	key         bool
}
func NewEncoder(e Emitter) *Encoder {
	if e == nil {
		panic("objconv: the emitter is nil")
	}
	return &Encoder{Emitter: e}
}
func (e Encoder) Encode(v interface{}) (err error) {
	if err = e.encodeMapValueMaybe(); err != nil {
		return
	}
	switch x := v.(type) {
	case nil:
		return e.Emitter.EmitNil()
	case bool:
		return e.Emitter.EmitBool(x)
	case int:
		return e.Emitter.EmitInt(int64(x), 0)
	case int8:
		return e.Emitter.EmitInt(int64(x), 8)
	case int16:
		return e.Emitter.EmitInt(int64(x), 16)
	case int32:
		return e.Emitter.EmitInt(int64(x), 32)
	case int64:
		return e.Emitter.EmitInt(x, 64)
	case uint8:
		return e.Emitter.EmitUint(uint64(x), 8)
	case uint16:
		return e.Emitter.EmitUint(uint64(x), 16)
	case uint32:
		return e.Emitter.EmitUint(uint64(x), 32)
	case uint64:
		return e.Emitter.EmitUint(x, 64)
	case string:
		return e.Emitter.EmitString(x)
	case []byte:
		return e.Emitter.EmitBytes(x)
	case time.Time:
		return e.Emitter.EmitTime(x)
	case time.Duration:
		return e.Emitter.EmitDuration(x)
	case []string:
		return e.encodeSliceOfString(x)
	case []interface{}:
		return e.encodeSliceOfInterface(x)
	case map[string]string:
		return e.encodeMapStringString(x)
	case map[string]interface{}:
		return e.encodeMapStringInterface(x)
	case map[interface{}]interface{}:
		return e.encodeMapInterfaceInterface(x)
	case *bool:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitBool(*x)
	case *int:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitInt(int64(*x), int(8*unsafe.Sizeof(0)))
	case *int8:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitInt(int64(*x), 8)
	case *int16:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitInt(int64(*x), 16)
	case *int32:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitInt(int64(*x), 32)
	case *int64:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitInt(*x, 64)
	case *uint8:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitUint(uint64(*x), 8)
	case *uint16:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitUint(uint64(*x), 16)
	case *uint32:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitUint(uint64(*x), 32)
	case *uint64:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitUint(*x, 64)
	case *string:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitString(*x)
	case *[]byte:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitBytes(*x)
	case *time.Time:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitTime(*x)
	case *time.Duration:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.Emitter.EmitDuration(*x)
	case *[]string:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.encodeSliceOfString(*x)
	case *[]interface{}:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.encodeSliceOfInterface(*x)
	case *map[string]string:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.encodeMapStringString(*x)
	case *map[string]interface{}:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.encodeMapStringInterface(*x)
	case *map[interface{}]interface{}:
		if x == nil {
			return e.Emitter.EmitNil()
		}
		return e.encodeMapInterfaceInterface(*x)
	case ValueEncoder:
		return x.EncodeValue(e)
	default:
		return e.encode(reflect.ValueOf(v))
	}
}
func (e *Encoder) encodeMapValueMaybe() (err error) {
	if e.key {
		e.key, err = false, e.Emitter.EmitMapValue()
	}
	return
}
func (e Encoder) encode(v reflect.Value) error {
	return encodeFuncOf(v.Type())(e, v)
}
func (e Encoder) encodeBool(v reflect.Value) error {
	return e.Emitter.EmitBool(v.Bool())
}
func (e Encoder) encodeInt(v reflect.Value) error {
	return e.Emitter.EmitInt(v.Int(), 0)
}
func (e Encoder) encodeInt8(v reflect.Value) error {
	return e.Emitter.EmitInt(v.Int(), 8)
}
func (e Encoder) encodeInt16(v reflect.Value) error {
	return e.Emitter.EmitInt(v.Int(), 16)
}
func (e Encoder) encodeInt32(v reflect.Value) error {
	return e.Emitter.EmitInt(v.Int(), 32)
}
func (e Encoder) encodeInt64(v reflect.Value) error {
	return e.Emitter.EmitInt(v.Int(), 64)
}
func (e Encoder) encodeUint(v reflect.Value) error {
	return e.Emitter.EmitUint(v.Uint(), 0)
}
func (e Encoder) encodeUint8(v reflect.Value) error {
	return e.Emitter.EmitUint(v.Uint(), 8)
}
func (e Encoder) encodeUint16(v reflect.Value) error {
	return e.Emitter.EmitUint(v.Uint(), 16)
}
func (e Encoder) encodeUint32(v reflect.Value) error {
	return e.Emitter.EmitUint(v.Uint(), 32)
}
func (e Encoder) encodeUint64(v reflect.Value) error {
	return e.Emitter.EmitUint(v.Uint(), 64)
}
func (e Encoder) encodeUintptr(v reflect.Value) error {
	return e.Emitter.EmitUint(v.Uint(), 0)
}
func (e Encoder) encodeFloat32(v reflect.Value) error {
	return e.Emitter.EmitFloat(v.Float(), 32)
}
func (e Encoder) encodeFloat64(v reflect.Value) error {
	return e.Emitter.EmitFloat(v.Float(), 64)
}
func (e Encoder) encodeString(v reflect.Value) error {
	return e.Emitter.EmitString(v.String())
}
func (e Encoder) encodeBytes(v reflect.Value) error {
	return e.Emitter.EmitBytes(v.Bytes())
}
func (e Encoder) encodeTime(v reflect.Value) error {
	var t time.Time
	if v.Kind() != reflect.Ptr {
		t = v.Interface().(time.Time)
	} else {
		if ptr := v.Interface().(*time.Time); ptr == nil {
			return e.Emitter.EmitNil()
		} else {
			t = *ptr
		}
	}
	return e.Emitter.EmitTime(t)
}
func (e Encoder) encodeDuration(v reflect.Value) error {
	return e.Emitter.EmitDuration(time.Duration(v.Int()))
}
func (e Encoder) encodeError(v reflect.Value) error {
	return e.Emitter.EmitError(v.Interface().(error))
}
func (e Encoder) encodeArray(v reflect.Value) error {
	return e.encodeArrayWith(v, encodeFuncOf(v.Type().Elem()))
}
func (e Encoder) encodeArrayWith(v reflect.Value, f encodeFunc) error {
	i := 0
	return e.EncodeArray(v.Len(), func(e Encoder) (err error) {
		err = f(e, v.Index(i))
		i++
		return
	})
}
func (e Encoder) encodeSliceOfString(a []string) error {
	i := 0
	return e.EncodeArray(len(a), func(e Encoder) (err error) {
		err = e.Emitter.EmitString(a[i])
		i++
		return
	})
}
func (e Encoder) encodeSliceOfInterface(a []interface{}) error {
	i := 0
	return e.EncodeArray(len(a), func(e Encoder) (err error) {
		err = e.Encode(a[i])
		i++
		return
	})
}
func (e Encoder) encodeMap(v reflect.Value) error {
	t := v.Type()
	kf := encodeFuncOf(t.Key())
	vf := encodeFuncOf(t.Elem())
	return e.encodeMapWith(v, kf, vf)
}
func (e Encoder) encodeMapWith(v reflect.Value, kf encodeFunc, vf encodeFunc) error {
	t := v.Type()
	if !e.SortMapKeys {
		switch {
		case t.ConvertibleTo(mapInterfaceInterfaceType):
			return e.encodeMapInterfaceInterfaceValue(v.Convert(mapInterfaceInterfaceType))
		case t.ConvertibleTo(mapStringInterfaceType):
			return e.encodeMapStringInterfaceValue(v.Convert(mapStringInterfaceType))
		case t.ConvertibleTo(mapStringStringType):
			return e.encodeMapStringStringValue(v.Convert(mapStringStringType))
		}
	}
	var k []reflect.Value
	var n = v.Len()
	var i = 0
	if n != 0 {
		k = v.MapKeys()
		if e.SortMapKeys {
			sortValues(t.Key(), k)
		}
	}
	return e.EncodeMap(n, func(ke Encoder, ve Encoder) (err error) {
		if err = kf(e, k[i]); err != nil {
			return
		}
		if err = e.Emitter.EmitMapValue(); err != nil {
			return
		}
		if err = vf(e, v.MapIndex(k[i])); err != nil {
			return
		}
		i++
		return
	})
}
func (e Encoder) encodeMapInterfaceInterfaceValue(v reflect.Value) error {
	return e.encodeMapInterfaceInterface(v.Interface().(map[interface{}]interface{}))
}
func (e Encoder) encodeMapInterfaceInterface(m map[interface{}]interface{}) (err error) {
	n := len(m)
	i := 0
	if err = e.Emitter.EmitMapBegin(n); err != nil {
		return
	}
	for k, v := range m {
		if i != 0 {
			if err = e.Emitter.EmitMapNext(); err != nil {
				return
			}
		}
		if err = e.Encode(k); err != nil {
			return
		}
		if err = e.Emitter.EmitMapValue(); err != nil {
			return
		}
		if err = e.Encode(v); err != nil {
			return
		}
		i++
	}
	return e.Emitter.EmitMapEnd()
}
func (e Encoder) encodeMapStringInterfaceValue(v reflect.Value) error {
	return e.encodeMapStringInterface(v.Interface().(map[string]interface{}))
}
func (e Encoder) encodeMapStringInterface(m map[string]interface{}) (err error) {
	n := len(m)
	i := 0
	if err = e.Emitter.EmitMapBegin(n); err != nil {
		return
	}
	for k, v := range m {
		if i != 0 {
			if err = e.Emitter.EmitMapNext(); err != nil {
				return
			}
		}
		if err = e.Emitter.EmitString(k); err != nil {
			return
		}
		if err = e.Emitter.EmitMapValue(); err != nil {
			return
		}
		if err = e.Encode(v); err != nil {
			return
		}
		i++
	}
	return e.Emitter.EmitMapEnd()
}
func (e Encoder) encodeMapStringStringValue(v reflect.Value) error {
	return e.encodeMapStringString(v.Interface().(map[string]string))
}
func (e Encoder) encodeMapStringString(m map[string]string) (err error) {
	n := len(m)
	i := 0
	if err = e.Emitter.EmitMapBegin(n); err != nil {
		return
	}
	for k, v := range m {
		if i != 0 {
			if err = e.Emitter.EmitMapNext(); err != nil {
				return
			}
		}
		if err = e.Emitter.EmitString(k); err != nil {
			return
		}
		if err = e.Emitter.EmitMapValue(); err != nil {
			return
		}
		if err = e.Emitter.EmitString(v); err != nil {
			return
		}
		i++
	}
	return e.Emitter.EmitMapEnd()
}
func (e Encoder) encodeStruct(v reflect.Value) error {
	return e.encodeStructWith(v, structCache.lookup(v.Type()))
}
func (e Encoder) encodeStructWith(v reflect.Value, s *structType) (err error) {
	n := 0
	for i := range s.fields {
		f := &s.fields[i]
		if !f.omit(v.FieldByIndex(f.index)) {
			n++
		}
	}
	if err = e.Emitter.EmitMapBegin(n); err != nil {
		return
	}
	n = 0
	for i := range s.fields {
		f := &s.fields[i]
		if fv := v.FieldByIndex(f.index); !f.omit(fv) {
			if n != 0 {
				if err = e.Emitter.EmitMapNext(); err != nil {
					return
				}
			}
			if err = e.Emitter.EmitString(f.name); err != nil {
				return
			}
			if err = e.Emitter.EmitMapValue(); err != nil {
				return
			}
			if err = f.encode(e, fv); err != nil {
				return
			}
			n++
		}
	}
	return e.Emitter.EmitMapEnd()
}
func (e Encoder) encodePointer(v reflect.Value) error {
	return e.encodePointerWith(v, encodeFuncOf(v.Type().Elem()))
}
func (e Encoder) encodePointerWith(v reflect.Value, f encodeFunc) error {
	if v.IsNil() {
		return e.Emitter.EmitNil()
	}
	return f(e, v.Elem())
}
func (e Encoder) encodeInterface(v reflect.Value) error {
	if v.IsNil() {
		return e.Emitter.EmitNil()
	}
	return e.encode(v.Elem())
}
func (e Encoder) encodeEncoder(v reflect.Value) error {
	return v.Interface().(ValueEncoder).EncodeValue(e)
}
func (e Encoder) encodeMarshaler(v reflect.Value) error {
	if isTextEmitter(e.Emitter) {
		return e.encodeTextMarshaler(v)
	}
	return e.encodeBinaryMarshaler(v)
}
func (e Encoder) encodeBinaryMarshaler(v reflect.Value) error {
	b, err := v.Interface().(encoding.BinaryMarshaler).MarshalBinary()
	if err == nil {
		err = e.Emitter.EmitBytes(b)
	}
	return err
}
func (e Encoder) encodeTextMarshaler(v reflect.Value) error {
	b, err := v.Interface().(encoding.TextMarshaler).MarshalText()
	if err == nil {
		err = e.Emitter.EmitString(stringNoCopy(b))
	}
	return err
}
func (e Encoder) encodeUnsupported(v reflect.Value) error {
	return fmt.Errorf("objconv: the encoder doesn't support values of type %s", v.Type())
}