// 0 terminates the structure.
type encoderState struct {
b *bytes.Buffer
- err os.Error // error encountered during encoding;
+ err os.Error // error encountered during encoding.
+ inArray bool // encoding an array element
fieldnum int // the last field number written.
buf [1 + uint64Size]byte // buffer used by the encoder; here to avoid allocation.
}
func encBool(i *encInstr, state *encoderState, p unsafe.Pointer) {
b := *(*bool)(p)
- if b {
+ if b || state.inArray {
state.update(i)
- encodeUint(state, 1)
+ if b {
+ encodeUint(state, 1)
+ } else {
+ encodeUint(state, 0)
+ }
}
}
func encInt(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := int64(*(*int)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeInt(state, v)
}
func encUint(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uint)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeUint(state, v)
}
func encInt8(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := int64(*(*int8)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeInt(state, v)
}
func encUint8(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uint8)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeUint(state, v)
}
func encInt16(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := int64(*(*int16)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeInt(state, v)
}
func encUint16(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uint16)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeUint(state, v)
}
func encInt32(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := int64(*(*int32)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeInt(state, v)
}
func encUint32(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uint32)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeUint(state, v)
}
func encInt64(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := *(*int64)(p)
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeInt(state, v)
}
func encUint64(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := *(*uint64)(p)
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeUint(state, v)
}
func encUintptr(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uintptr)(p))
- if v != 0 {
+ if v != 0 || state.inArray {
state.update(i)
encodeUint(state, v)
}
func encFloat(i *encInstr, state *encoderState, p unsafe.Pointer) {
f := float(*(*float)(p))
- if f != 0 {
+ if f != 0 || state.inArray {
v := floatBits(float64(f))
state.update(i)
encodeUint(state, v)
func encFloat32(i *encInstr, state *encoderState, p unsafe.Pointer) {
f := float32(*(*float32)(p))
- if f != 0 {
+ if f != 0 || state.inArray {
v := floatBits(float64(f))
state.update(i)
encodeUint(state, v)
func encFloat64(i *encInstr, state *encoderState, p unsafe.Pointer) {
f := *(*float64)(p)
- if f != 0 {
+ if f != 0 || state.inArray {
state.update(i)
v := floatBits(f)
encodeUint(state, v)
// Byte arrays are encoded as an unsigned count followed by the raw bytes.
func encUint8Array(i *encInstr, state *encoderState, p unsafe.Pointer) {
b := *(*[]byte)(p)
- if len(b) > 0 {
+ if len(b) > 0 || state.inArray {
state.update(i)
encodeUint(state, uint64(len(b)))
state.b.Write(b)
// Strings are encoded as an unsigned count followed by the raw bytes.
func encString(i *encInstr, state *encoderState, p unsafe.Pointer) {
s := *(*string)(p)
- if len(s) > 0 {
+ if len(s) > 0 || state.inArray {
state.update(i)
encodeUint(state, uint64(len(s)))
io.WriteString(state.b, s)
state := new(encoderState)
state.b = b
state.fieldnum = -1
+ state.inArray = true
encodeUint(state, uint64(length))
for i := 0; i < length && state.err == nil; i++ {
elemp := p