diff --git a/encode.go b/encode.go index a3a6b5c..0720f34 100644 --- a/encode.go +++ b/encode.go @@ -102,6 +102,11 @@ func (e *Encoder) EncodeWithOption(v interface{}, opts ...EncodeOption) error { if err := e.encode(v); err != nil { return err } + if e.enabledIndent { + e.buf = e.buf[:len(e.buf)-2] + } else { + e.buf = e.buf[:len(e.buf)-1] + } e.buf = append(e.buf, '\n') if _, err := e.w.Write(e.buf); err != nil { return err @@ -147,16 +152,11 @@ func (e *Encoder) encodeForMarshal(v interface{}) ([]byte, error) { return nil, err } if e.enabledIndent { - last := len(e.buf) - 1 - if e.buf[last] == '\n' { - last-- - } - length := last + 1 - copied := make([]byte, length) - copy(copied, e.buf[0:length]) + copied := make([]byte, len(e.buf)-2) + copy(copied, e.buf) return copied, nil } - copied := make([]byte, len(e.buf)) + copied := make([]byte, len(e.buf)-1) copy(copied, e.buf) return copied, nil } @@ -164,6 +164,11 @@ func (e *Encoder) encodeForMarshal(v interface{}) ([]byte, error) { func (e *Encoder) encode(v interface{}) error { if v == nil { e.encodeNull() + if e.enabledIndent { + e.encodeBytes([]byte{',', '\n'}) + } else { + e.encodeByte(',') + } return nil } header := (*interfaceHeader)(unsafe.Pointer(&v)) diff --git a/encode_compile.go b/encode_compile.go index 460e37f..0cdac85 100644 --- a/encode_compile.go +++ b/encode_compile.go @@ -427,32 +427,60 @@ func (e *Encoder) typeToHeaderType(op opType) opType { switch op { case opInt: return opStructFieldHeadInt + case opIntIndent: + return opStructFieldHeadIntIndent case opInt8: return opStructFieldHeadInt8 + case opInt8Indent: + return opStructFieldHeadInt8Indent case opInt16: return opStructFieldHeadInt16 + case opInt16Indent: + return opStructFieldHeadInt16Indent case opInt32: return opStructFieldHeadInt32 + case opInt32Indent: + return opStructFieldHeadInt32Indent case opInt64: return opStructFieldHeadInt64 + case opInt64Indent: + return opStructFieldHeadInt64Indent case opUint: return opStructFieldHeadUint + case opUintIndent: + return opStructFieldHeadUintIndent case opUint8: return opStructFieldHeadUint8 + case opUint8Indent: + return opStructFieldHeadUint8Indent case opUint16: return opStructFieldHeadUint16 + case opUint16Indent: + return opStructFieldHeadUint16Indent case opUint32: return opStructFieldHeadUint32 + case opUint32Indent: + return opStructFieldHeadUint32Indent case opUint64: return opStructFieldHeadUint64 + case opUint64Indent: + return opStructFieldHeadUint64Indent case opFloat32: return opStructFieldHeadFloat32 + case opFloat32Indent: + return opStructFieldHeadFloat32Indent case opFloat64: return opStructFieldHeadFloat64 + case opFloat64Indent: + return opStructFieldHeadFloat64Indent case opString: return opStructFieldHeadString + case opStringIndent: + return opStructFieldHeadStringIndent case opBool: return opStructFieldHeadBool + case opBoolIndent: + return opStructFieldHeadBoolIndent case opMapHead: return opStructFieldHeadMap case opMapHeadLoad: @@ -475,8 +503,12 @@ func (e *Encoder) typeToHeaderType(op opType) opType { return opStructFieldHeadStructIndent case opMarshalJSON: return opStructFieldHeadMarshalJSON + case opMarshalJSONIndent: + return opStructFieldHeadMarshalJSONIndent case opMarshalText: return opStructFieldHeadMarshalText + case opMarshalTextIndent: + return opStructFieldHeadMarshalTextIndent } return opStructFieldHead } @@ -516,32 +548,60 @@ func (e *Encoder) typeToFieldType(code *opcode) opType { } case opInt: return opStructFieldInt + case opIntIndent: + return opStructFieldIntIndent case opInt8: return opStructFieldInt8 + case opInt8Indent: + return opStructFieldInt8Indent case opInt16: return opStructFieldInt16 + case opInt16Indent: + return opStructFieldInt16Indent case opInt32: return opStructFieldInt32 + case opInt32Indent: + return opStructFieldInt32Indent case opInt64: return opStructFieldInt64 + case opInt64Indent: + return opStructFieldInt64Indent case opUint: return opStructFieldUint + case opUintIndent: + return opStructFieldUintIndent case opUint8: return opStructFieldUint8 + case opUint8Indent: + return opStructFieldUint8Indent case opUint16: return opStructFieldUint16 + case opUint16Indent: + return opStructFieldUint16Indent case opUint32: return opStructFieldUint32 + case opUint32Indent: + return opStructFieldUint32Indent case opUint64: return opStructFieldUint64 + case opUint64Indent: + return opStructFieldUint64Indent case opFloat32: return opStructFieldFloat32 + case opFloat32Indent: + return opStructFieldFloat32Indent case opFloat64: return opStructFieldFloat64 + case opFloat64Indent: + return opStructFieldFloat64Indent case opString: return opStructFieldString + case opStringIndent: + return opStructFieldStringIndent case opBool: return opStructFieldBool + case opBoolIndent: + return opStructFieldBoolIndent case opMapHead: return opStructFieldMap case opMapHeadLoad: @@ -564,8 +624,12 @@ func (e *Encoder) typeToFieldType(code *opcode) opType { return opStructFieldStructIndent case opMarshalJSON: return opStructFieldMarshalJSON + case opMarshalJSONIndent: + return opStructFieldMarshalJSONIndent case opMarshalText: return opStructFieldMarshalText + case opMarshalTextIndent: + return opStructFieldMarshalTextIndent } return opStructField } diff --git a/encode_opcode.go b/encode_opcode.go index 4b3ed1e..31030c5 100644 --- a/encode_opcode.go +++ b/encode_opcode.go @@ -52,6 +52,9 @@ func copyOpcode(code *opcode) *opcode { } func newOpCodeWithNext(ctx *encodeCompileContext, op opType, next *opcode) *opcode { + if op != opEnd && ctx.withIndent { + op = op.toIndent() + } return &opcode{ op: op, typ: ctx.typ, diff --git a/encode_vm.go b/encode_vm.go index 2771328..efb74c8 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -41,42 +41,99 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { for { switch code.op { - case opPtr: + default: + return fmt.Errorf("failed to handle opcode. doesn't implement %s", code.op) + case opPtr, opPtrIndent: ptr := load(ctxptr, code.idx) code = code.next store(ctxptr, code.idx, e.ptrToPtr(ptr)) case opInt: e.encodeInt(e.ptrToInt(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opIntIndent: + e.encodeInt(e.ptrToInt(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opInt8: e.encodeInt8(e.ptrToInt8(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opInt8Indent: + e.encodeInt8(e.ptrToInt8(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opInt16: e.encodeInt16(e.ptrToInt16(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opInt16Indent: + e.encodeInt16(e.ptrToInt16(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opInt32: e.encodeInt32(e.ptrToInt32(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opInt32Indent: + e.encodeInt32(e.ptrToInt32(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opInt64: e.encodeInt64(e.ptrToInt64(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opInt64Indent: + e.encodeInt64(e.ptrToInt64(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opUint: e.encodeUint(e.ptrToUint(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opUintIndent: + e.encodeUint(e.ptrToUint(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opUint8: e.encodeUint8(e.ptrToUint8(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opUint8Indent: + e.encodeUint8(e.ptrToUint8(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opUint16: e.encodeUint16(e.ptrToUint16(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opUint16Indent: + e.encodeUint16(e.ptrToUint16(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opUint32: e.encodeUint32(e.ptrToUint32(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opUint32Indent: + e.encodeUint32(e.ptrToUint32(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opUint64: e.encodeUint64(e.ptrToUint64(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opUint64Indent: + e.encodeUint64(e.ptrToUint64(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opFloat32: e.encodeFloat32(e.ptrToFloat32(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opFloat32Indent: + e.encodeFloat32(e.ptrToFloat32(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opFloat64: v := e.ptrToFloat64(load(ctxptr, code.idx)) @@ -87,12 +144,34 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeFloat64(v) + e.encodeByte(',') + code = code.next + case opFloat64Indent: + v := e.ptrToFloat64(load(ctxptr, code.idx)) + if math.IsInf(v, 0) || math.IsNaN(v) { + return &UnsupportedValueError{ + Value: reflect.ValueOf(v), + Str: strconv.FormatFloat(v, 'g', -1, 64), + } + } + e.encodeFloat64(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opString: e.encodeString(e.ptrToString(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opStringIndent: + e.encodeString(e.ptrToString(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opBool: e.encodeBool(e.ptrToBool(load(ctxptr, code.idx))) + e.encodeByte(',') + code = code.next + case opBoolIndent: + e.encodeBool(e.ptrToBool(load(ctxptr, code.idx))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opBytes: ptr := load(ctxptr, code.idx) @@ -102,11 +181,23 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeByteSlice(e.ptrToBytes(ptr)) } + e.encodeByte(',') + code = code.next + case opBytesIndent: + ptr := load(ctxptr, code.idx) + header := (*reflect.SliceHeader)(unsafe.Pointer(ptr)) + if ptr == 0 || header.Data == 0 { + e.encodeNull() + } else { + e.encodeByteSlice(e.ptrToBytes(ptr)) + } + e.encodeBytes([]byte{',', '\n'}) code = code.next case opInterface: ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.next break } @@ -124,6 +215,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { rv := reflect.ValueOf(v) if rv.IsNil() { e.encodeNull() + e.encodeByte(',') code = code.next break } @@ -183,7 +275,89 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { code = c recursiveLevel++ - case opInterfaceEnd: + case opInterfaceIndent: + ptr := load(ctxptr, code.idx) + if ptr == 0 { + e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) + code = code.next + break + } + v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ + typ: code.typ, + ptr: unsafe.Pointer(ptr), + })) + if _, exists := seenPtr[ptr]; exists { + return &UnsupportedValueError{ + Value: reflect.ValueOf(v), + Str: fmt.Sprintf("encountered a cycle via %s", code.typ), + } + } + seenPtr[ptr] = struct{}{} + rv := reflect.ValueOf(v) + if rv.IsNil() { + e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) + code = code.next + break + } + vv := rv.Interface() + header := (*interfaceHeader)(unsafe.Pointer(&vv)) + typ := header.typ + if typ.Kind() == reflect.Ptr { + typ = typ.Elem() + } + var c *opcode + if typ.Kind() == reflect.Map { + code, err := e.compileMap(&encodeCompileContext{ + typ: typ, + root: code.root, + withIndent: e.enabledIndent, + indent: code.indent, + }, false) + if err != nil { + return err + } + c = code + } else { + code, err := e.compile(&encodeCompileContext{ + typ: typ, + root: code.root, + withIndent: e.enabledIndent, + indent: code.indent, + }) + if err != nil { + return err + } + c = code + } + + beforeLastCode := c.beforeLastCode() + lastCode := beforeLastCode.next + lastCode.idx = beforeLastCode.idx + uintptrSize + totalLength := uintptr(code.totalLength()) + nextTotalLength := uintptr(c.totalLength()) + curlen := uintptr(len(ctx.ptrs)) + offsetNum := ptrOffset / uintptrSize + oldOffset := ptrOffset + ptrOffset += totalLength * uintptrSize + + newLen := offsetNum + totalLength + nextTotalLength + if curlen < newLen { + ctx.ptrs = append(ctx.ptrs, make([]uintptr, newLen-curlen)...) + } + ctxptr = ctx.ptr() + ptrOffset // assign new ctxptr + + store(ctxptr, 0, uintptr(header.ptr)) + store(ctxptr, lastCode.idx, oldOffset) + + // link lastCode ( opInterfaceEnd ) => code.next + lastCode.op = opInterfaceEnd + lastCode.next = code.next + + code = c + recursiveLevel++ + case opInterfaceEnd, opInterfaceEndIndent: recursiveLevel-- // restore ctxptr offset := load(ctxptr, code.idx) @@ -225,6 +399,44 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeBytes(buf.Bytes()) + e.encodeByte(',') + code = code.next + case opMarshalJSONIndent: + ptr := load(ctxptr, code.idx) + v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ + typ: code.typ, + ptr: unsafe.Pointer(ptr), + })) + b, err := v.(Marshaler).MarshalJSON() + if err != nil { + return &MarshalerError{ + Type: rtype2type(code.typ), + Err: err, + } + } + if len(b) == 0 { + return errUnexpectedEndOfJSON( + fmt.Sprintf("error calling MarshalJSON for type %s", code.typ), + 0, + ) + } + var buf bytes.Buffer + if e.enabledIndent { + if err := encodeWithIndent( + &buf, + b, + string(e.prefix)+string(bytes.Repeat(e.indentStr, code.indent)), + string(e.indentStr), + ); err != nil { + return err + } + } else { + if err := compact(&buf, b, e.enabledHTMLEscape); err != nil { + return err + } + } + e.encodeBytes(buf.Bytes()) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opMarshalText: ptr := load(ctxptr, code.idx) @@ -232,8 +444,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { p := unsafe.Pointer(ptr) if p == nil { e.encodeNull() + e.encodeByte(',') } else if isPtr && *(*unsafe.Pointer)(p) == nil { e.encodeBytes([]byte{'"', '"'}) + e.encodeByte(',') } else { if isPtr && code.typ.Elem().Implements(marshalTextType) { p = *(*unsafe.Pointer)(p) @@ -250,6 +464,36 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') + } + code = code.next + case opMarshalTextIndent: + ptr := load(ctxptr, code.idx) + isPtr := code.typ.Kind() == reflect.Ptr + p := unsafe.Pointer(ptr) + if p == nil { + e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) + } else if isPtr && *(*unsafe.Pointer)(p) == nil { + e.encodeBytes([]byte{'"', '"'}) + e.encodeBytes([]byte{',', '\n'}) + } else { + if isPtr && code.typ.Elem().Implements(marshalTextType) { + p = *(*unsafe.Pointer)(p) + } + v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ + typ: code.typ, + ptr: p, + })) + bytes, err := v.(encoding.TextMarshaler).MarshalText() + if err != nil { + return &MarshalerError{ + Type: rtype2type(code.typ), + Err: err, + } + } + e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opSliceHead: @@ -257,17 +501,18 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { header := (*reflect.SliceHeader)(unsafe.Pointer(p)) if p == 0 || header.Data == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { - e.encodeByte('[') store(ctxptr, code.elemIdx, 0) store(ctxptr, code.length, uintptr(header.Len)) store(ctxptr, code.idx, header.Data) if header.Len > 0 { + e.encodeByte('[') code = code.next store(ctxptr, code.idx, header.Data) } else { - e.encodeByte(']') + e.encodeBytes([]byte{'[', ']', ','}) code = code.end.next } } @@ -276,14 +521,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { length := load(ctxptr, code.length) idx++ if idx < length { - e.encodeByte(',') store(ctxptr, code.elemIdx, idx) data := load(ctxptr, code.headIdx) size := code.size code = code.next store(ctxptr, code.idx, data+idx*size) } else { - e.encodeByte(']') + last := len(e.buf) - 1 + e.buf[last] = ']' + e.encodeByte(',') code = code.end.next } case opSliceHeadIndent: @@ -291,6 +537,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { header := (*reflect.SliceHeader)(unsafe.Pointer(p)) @@ -304,7 +551,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { store(ctxptr, code.idx, header.Data) } else { e.encodeIndent(code.indent) - e.encodeBytes([]byte{'[', ']'}) + e.encodeBytes([]byte{'[', ']', '\n'}) code = code.end.next } } @@ -313,6 +560,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { header := (*reflect.SliceHeader)(unsafe.Pointer(p)) @@ -326,7 +574,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { store(ctxptr, code.idx, header.Data) } else { e.encodeIndent(code.indent) - e.encodeBytes([]byte{'[', ']'}) + e.encodeBytes([]byte{'[', ']', ',', '\n'}) code = code.end.next } } @@ -335,7 +583,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { length := load(ctxptr, code.length) idx++ if idx < length { - e.encodeBytes([]byte{',', '\n'}) e.encodeIndent(code.indent + 1) store(ctxptr, code.elemIdx, idx) data := load(ctxptr, code.headIdx) @@ -343,9 +590,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { code = code.next store(ctxptr, code.idx, data+idx*size) } else { + e.buf = e.buf[:len(e.buf)-2] e.encodeByte('\n') e.encodeIndent(code.indent) - e.encodeByte(']') + e.encodeBytes([]byte{']', ',', '\n'}) code = code.end.next } case opRootSliceElemIndent: @@ -353,7 +601,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { length := load(ctxptr, code.length) idx++ if idx < length { - e.encodeBytes([]byte{',', '\n'}) e.encodeIndent(code.indent + 1) store(ctxptr, code.elemIdx, idx) code = code.next @@ -369,15 +616,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { - e.encodeByte('[') if code.length > 0 { + e.encodeByte('[') store(ctxptr, code.elemIdx, 0) code = code.next store(ctxptr, code.idx, p) } else { - e.encodeByte(']') + e.encodeBytes([]byte{'[', ']', ','}) code = code.end.next } } @@ -385,14 +633,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { idx := load(ctxptr, code.elemIdx) idx++ if idx < code.length { - e.encodeByte(',') store(ctxptr, code.elemIdx, idx) p := load(ctxptr, code.headIdx) size := code.size code = code.next store(ctxptr, code.idx, p+idx*size) } else { - e.encodeByte(']') + last := len(e.buf) - 1 + e.buf[last] = ']' + e.encodeByte(',') code = code.end.next } case opArrayHeadIndent: @@ -400,17 +649,18 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { - e.encodeBytes([]byte{'[', '\n'}) if code.length > 0 { + e.encodeBytes([]byte{'[', '\n'}) e.encodeIndent(code.indent + 1) store(ctxptr, code.elemIdx, 0) code = code.next store(ctxptr, code.idx, p) } else { e.encodeIndent(code.indent) - e.encodeBytes([]byte{']', '\n'}) + e.encodeBytes([]byte{'[', ']', ',', '\n'}) code = code.end.next } } @@ -418,7 +668,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { idx := load(ctxptr, code.elemIdx) idx++ if idx < code.length { - e.encodeBytes([]byte{',', '\n'}) e.encodeIndent(code.indent + 1) store(ctxptr, code.elemIdx, idx) p := load(ctxptr, code.headIdx) @@ -426,20 +675,22 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { code = code.next store(ctxptr, code.idx, p+idx*size) } else { + e.buf = e.buf[:len(e.buf)-2] e.encodeByte('\n') e.encodeIndent(code.indent) - e.encodeBytes([]byte{']', '\n'}) + e.encodeBytes([]byte{']', ',', '\n'}) code = code.end.next } case opMapHead: ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { - e.encodeByte('{') mlen := maplen(unsafe.Pointer(ptr)) if mlen > 0 { + e.encodeByte('{') iter := mapiterinit(code.typ, unsafe.Pointer(ptr)) ctx.keepRefs = append(ctx.keepRefs, iter) store(ctxptr, code.elemIdx, 0) @@ -456,7 +707,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { store(ctxptr, code.next.idx, uintptr(key)) code = code.next } else { - e.encodeByte('}') + e.encodeBytes([]byte{'{', '}', ','}) code = code.end.next } } @@ -464,18 +715,20 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { // load pointer ptr = uintptr(*(*unsafe.Pointer)(unsafe.Pointer(ptr))) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } - e.encodeByte('{') mlen := maplen(unsafe.Pointer(ptr)) if mlen > 0 { + e.encodeByte('{') iter := mapiterinit(code.typ, unsafe.Pointer(ptr)) ctx.keepRefs = append(ctx.keepRefs, iter) store(ctxptr, code.elemIdx, 0) @@ -492,7 +745,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } code = code.next } else { - e.encodeByte('}') + e.encodeBytes([]byte{'{', '}', ','}) code = code.end.next } } @@ -502,14 +755,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { idx++ if e.unorderedMap { if idx < length { - e.encodeByte(',') iter := unsafe.Pointer(load(ctxptr, code.mapIter)) store(ctxptr, code.elemIdx, idx) key := mapiterkey(iter) store(ctxptr, code.next.idx, uintptr(key)) code = code.next } else { - e.encodeByte('}') + last := len(e.buf) - 1 + e.buf[last] = '}' + e.encodeByte(',') code = code.end.next } } else { @@ -527,7 +781,8 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } case opMapValue: if e.unorderedMap { - e.encodeByte(':') + last := len(e.buf) - 1 + e.buf[last] = ':' } else { posPtr := (*[]int)(unsafe.Pointer(load(ctxptr, code.end.mapPos))) *posPtr = append(*posPtr, len(e.buf)) @@ -566,15 +821,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { }) buf := e.buf[pos[0]:] buf = buf[:0] - for idx, kv := range kvs { - if idx != 0 { - buf = append(buf, ',') - } + for _, kv := range kvs { buf = append(buf, []byte(kv.key)...) - buf = append(buf, ':') + buf[len(buf)-1] = ':' buf = append(buf, []byte(kv.value)...) } - buf = append(buf, '}') + buf[len(buf)-1] = '}' + buf = append(buf, ',') e.buf = e.buf[:pos[0]] e.buf = append(e.buf, buf...) code = code.next @@ -583,6 +836,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { mlen := maplen(unsafe.Pointer(ptr)) @@ -609,7 +863,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { code = code.next } else { e.encodeIndent(code.indent) - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) code = code.end.next } } @@ -625,6 +879,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next break } @@ -652,7 +907,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { code = code.next } else { e.encodeIndent(code.indent) - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) code = code.end.next } } @@ -662,7 +917,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { idx++ if e.unorderedMap { if idx < length { - e.encodeBytes([]byte{',', '\n'}) e.encodeIndent(code.indent) store(ctxptr, code.elemIdx, idx) iter := unsafe.Pointer(load(ctxptr, code.mapIter)) @@ -670,9 +924,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { store(ctxptr, code.next.idx, uintptr(key)) code = code.next } else { - e.encodeByte('\n') + last := len(e.buf) - 1 + e.buf[last] = '\n' e.encodeIndent(code.indent - 1) - e.encodeByte('}') + e.encodeBytes([]byte{'}', ',', '\n'}) code = code.end.next } } else { @@ -728,22 +983,20 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { }) buf := e.buf[pos[0]:] buf = buf[:0] - for idx, kv := range kvs { - if idx != 0 { - buf = append(buf, []byte{',', '\n'}...) - } - + for _, kv := range kvs { buf = append(buf, e.prefix...) buf = append(buf, bytes.Repeat(e.indentStr, code.indent+1)...) buf = append(buf, []byte(kv.key)...) - buf = append(buf, []byte{':', ' '}...) + buf[len(buf)-2] = ':' + buf[len(buf)-1] = ' ' buf = append(buf, []byte(kv.value)...) } + buf = buf[:len(buf)-2] buf = append(buf, '\n') buf = append(buf, e.prefix...) buf = append(buf, bytes.Repeat(e.indentStr, code.indent)...) - buf = append(buf, '}') + buf = append(buf, '}', ',', '\n') e.buf = e.buf[:pos[0]] e.buf = append(e.buf, buf...) code = code.next @@ -812,8 +1065,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHead { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { @@ -837,6 +1091,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -847,14 +1102,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadInt { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeInt(e.ptrToInt(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadInt: @@ -867,12 +1124,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt(e.ptrToInt(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadInt8: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -883,14 +1142,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadInt8 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeInt8(e.ptrToInt8(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadInt8: @@ -903,12 +1164,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt8(e.ptrToInt8(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadInt16: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -919,14 +1182,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadInt16 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeInt16(e.ptrToInt16(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadInt16: @@ -939,12 +1204,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt16(e.ptrToInt16(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadInt32: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -955,14 +1222,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadInt32 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeInt32(e.ptrToInt32(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadInt32: @@ -975,12 +1244,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt32(e.ptrToInt32(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadInt64: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -991,14 +1262,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadInt64 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeInt64(e.ptrToInt64(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadInt64: @@ -1011,12 +1284,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt64(e.ptrToInt64(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadUint: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1027,14 +1302,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadUint { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeUint(e.ptrToUint(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadUint: @@ -1047,12 +1324,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint(e.ptrToUint(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadUint8: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1063,14 +1342,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadUint8 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeUint8(e.ptrToUint8(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadUint8: @@ -1083,12 +1364,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint8(e.ptrToUint8(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadUint16: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1099,14 +1382,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadUint16 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeUint16(e.ptrToUint16(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadUint16: @@ -1119,12 +1404,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint16(e.ptrToUint16(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadUint32: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1135,14 +1422,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadUint32 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeUint32(e.ptrToUint32(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadUint32: @@ -1155,12 +1444,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint32(e.ptrToUint32(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadUint64: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1171,14 +1462,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadUint64 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeUint64(e.ptrToUint64(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadUint64: @@ -1191,12 +1484,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint64(e.ptrToUint64(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadFloat32: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1207,14 +1502,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadFloat32 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeFloat32(e.ptrToFloat32(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadFloat32: @@ -1227,12 +1524,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeFloat32(e.ptrToFloat32(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadFloat64: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1243,8 +1542,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadFloat64 { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { @@ -1258,6 +1558,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeByte('{') e.encodeKey(code) e.encodeFloat64(v) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadFloat64: @@ -1277,12 +1578,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeFloat64(v) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadString: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1293,14 +1596,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadString { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(e.ptrToString(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadString: @@ -1313,12 +1618,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(e.ptrToString(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadBool: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1329,14 +1636,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadBool { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeBool(e.ptrToBool(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadBool: @@ -1349,12 +1658,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeBool(e.ptrToBool(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadBytes: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1365,14 +1676,16 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadBytes { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ','}) } code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeByteSlice(e.ptrToBytes(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadBytes: @@ -1385,12 +1698,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeByteSlice(e.ptrToBytes(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadArray: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1401,8 +1716,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { if code.op == opStructFieldPtrHeadArray { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'[', ']'}) + e.encodeBytes([]byte{'[', ']', ','}) } code = code.end.next } else { @@ -1429,6 +1745,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1440,8 +1757,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 { if code.op == opStructFieldPtrHeadSlice { e.encodeNull() + e.encodeByte(',') } else { - e.encodeBytes([]byte{'[', ']'}) + e.encodeBytes([]byte{'[', ']', ','}) } code = code.end.next } else { @@ -1469,6 +1787,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1478,6 +1797,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -1510,6 +1830,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { return err } e.encodeBytes(buf.Bytes()) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadMarshalJSON: @@ -1549,12 +1870,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { return err } e.encodeBytes(buf.Bytes()) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadMarshalText: p := load(ctxptr, code.idx) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1564,6 +1887,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -1575,6 +1899,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { rv := reflect.ValueOf(v) if rv.Type().Kind() == reflect.Interface && rv.IsNil() { e.encodeNull() + e.encodeByte(',') code = code.end break } @@ -1586,6 +1911,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadMarshalText: @@ -1604,6 +1930,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { rv := reflect.ValueOf(v) if rv.Type().Kind() == reflect.Interface && rv.IsNil() { e.encodeNull() + e.encodeByte(',') code = code.end.next break } @@ -1615,6 +1942,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadIndent: @@ -1628,15 +1956,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else if code.next == code.end { // not exists fields e.encodeIndent(code.indent) - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) code = code.end.next store(ctxptr, code.idx, ptr) } else { - e.encodeIndent(code.indent) e.encodeBytes([]byte{'{', '\n'}) e.encodeIndent(code.indent + 1) e.encodeKey(code) @@ -1653,8 +1981,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if code.op == opStructFieldPtrHeadIntIndent { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) } else { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) } code = code.end.next } else { @@ -1663,6 +1992,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt(e.ptrToInt(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadInt8Indent: @@ -1673,6 +2003,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1681,6 +2012,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt8(e.ptrToInt8(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadInt16Indent: @@ -1690,6 +2022,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1698,6 +2031,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt16(e.ptrToInt16(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadInt32Indent: @@ -1708,6 +2042,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1716,6 +2051,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt32(e.ptrToInt32(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadInt64Indent: @@ -1726,6 +2062,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1734,6 +2071,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt64(e.ptrToInt64(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadUintIndent: @@ -1744,6 +2082,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1752,6 +2091,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint(e.ptrToUint(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadUint8Indent: @@ -1762,6 +2102,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1770,6 +2111,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint8(e.ptrToUint8(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadUint16Indent: @@ -1780,6 +2122,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1788,6 +2131,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint16(e.ptrToUint16(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadUint32Indent: @@ -1798,6 +2142,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1806,6 +2151,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint32(e.ptrToUint32(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadUint64Indent: @@ -1816,6 +2162,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1824,6 +2171,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint64(e.ptrToUint64(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadFloat32Indent: @@ -1834,6 +2182,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1842,6 +2191,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeFloat32(e.ptrToFloat32(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadFloat64Indent: @@ -1852,6 +2202,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { v := e.ptrToFloat64(ptr) @@ -1867,6 +2218,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeFloat64(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringIndent: @@ -1877,6 +2229,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1885,6 +2238,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(e.ptrToString(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadBoolIndent: @@ -1895,6 +2249,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1903,6 +2258,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeBool(e.ptrToBool(ptr)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadBytesIndent: @@ -1913,6 +2269,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -1924,6 +2281,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeByte('"') e.encodeBytes(*(*[]byte)(unsafe.Pointer(&s))) e.encodeByte('"') + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadOmitEmpty: @@ -1936,6 +2294,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -1978,6 +2337,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -1987,6 +2347,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt(v) + e.encodeByte(',') code = code.next } } @@ -2007,6 +2368,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt(v) + e.encodeByte(',') code = code.next } } @@ -2020,6 +2382,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2029,6 +2392,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt8(v) + e.encodeByte(',') code = code.next } } @@ -2049,6 +2413,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt8(v) + e.encodeByte(',') code = code.next } } @@ -2062,6 +2427,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2071,6 +2437,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt16(v) + e.encodeByte(',') code = code.next } } @@ -2091,6 +2458,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt16(v) + e.encodeByte(',') code = code.next } } @@ -2104,6 +2472,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2113,6 +2482,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt32(v) + e.encodeByte(',') code = code.next } } @@ -2133,6 +2503,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt32(v) + e.encodeByte(',') code = code.next } } @@ -2146,6 +2517,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2155,6 +2527,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt64(v) + e.encodeByte(',') code = code.next } } @@ -2175,6 +2548,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeInt64(v) + e.encodeByte(',') code = code.next } } @@ -2188,6 +2562,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2197,6 +2572,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint(v) + e.encodeByte(',') code = code.next } } @@ -2217,6 +2593,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint(v) + e.encodeByte(',') code = code.next } } @@ -2230,6 +2607,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2239,6 +2617,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint8(v) + e.encodeByte(',') code = code.next } } @@ -2259,6 +2638,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint8(v) + e.encodeByte(',') code = code.next } } @@ -2272,6 +2652,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2281,6 +2662,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint16(v) + e.encodeByte(',') code = code.next } } @@ -2301,6 +2683,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint16(v) + e.encodeByte(',') code = code.next } } @@ -2314,6 +2697,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2323,6 +2707,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint32(v) + e.encodeByte(',') code = code.next } } @@ -2343,6 +2728,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint32(v) + e.encodeByte(',') code = code.next } } @@ -2356,6 +2742,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2365,6 +2752,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint64(v) + e.encodeByte(',') code = code.next } } @@ -2385,6 +2773,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeUint64(v) + e.encodeByte(',') code = code.next } } @@ -2398,6 +2787,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2407,6 +2797,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeFloat32(v) + e.encodeByte(',') code = code.next } } @@ -2427,6 +2818,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeFloat32(v) + e.encodeByte(',') code = code.next } } @@ -2440,6 +2832,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2455,6 +2848,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeFloat64(v) + e.encodeByte(',') code = code.next } } @@ -2481,6 +2875,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeFloat64(v) + e.encodeByte(',') code = code.next } } @@ -2494,6 +2889,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2503,6 +2899,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(v) + e.encodeByte(',') code = code.next } } @@ -2523,6 +2920,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(v) + e.encodeByte(',') code = code.next } } @@ -2536,6 +2934,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2545,6 +2944,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeBool(v) + e.encodeByte(',') code = code.next } } @@ -2565,6 +2965,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeBool(v) + e.encodeByte(',') code = code.next } } @@ -2578,6 +2979,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2587,6 +2989,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeByteSlice(v) + e.encodeByte(',') code = code.next } } @@ -2607,6 +3010,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeByteSlice(v) + e.encodeByte(',') code = code.next } } @@ -2620,6 +3024,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2651,6 +3056,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeBytes(buf.Bytes()) + e.encodeByte(',') code = code.next } } @@ -2694,6 +3100,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeBytes(buf.Bytes()) + e.encodeByte(',') code = code.next } } @@ -2708,6 +3115,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -2726,6 +3134,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') code = code.next } } @@ -2755,6 +3164,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') code = code.next } } @@ -2769,6 +3179,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2795,6 +3206,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2807,6 +3219,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -2821,6 +3234,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2833,6 +3247,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt8(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -2847,6 +3262,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2859,6 +3275,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt16(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -2873,6 +3290,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2885,6 +3303,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt32(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -2899,6 +3318,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2911,6 +3331,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeInt64(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -2925,6 +3346,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2937,6 +3359,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -2951,6 +3374,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2963,6 +3387,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint8(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -2977,6 +3402,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -2989,6 +3415,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint16(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -3003,6 +3430,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -3015,6 +3443,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint32(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -3029,6 +3458,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -3041,6 +3471,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeUint64(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -3055,6 +3486,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -3067,6 +3499,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeFloat32(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -3081,6 +3514,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -3099,6 +3533,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeFloat64(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -3113,6 +3548,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -3125,6 +3561,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -3139,6 +3576,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -3151,6 +3589,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeBool(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -3165,6 +3604,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeIndent(code.indent) @@ -3180,6 +3620,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeByte('"') e.encodeBytes(*(*[]byte)(unsafe.Pointer(&s))) e.encodeByte('"') + e.encodeBytes([]byte{',', '\n'}) code = code.next } } @@ -3193,6 +3634,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -3226,11 +3668,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagInt: @@ -3246,6 +3690,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagInt8: @@ -3258,11 +3703,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt8(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagInt8: @@ -3278,6 +3725,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt8(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagInt16: @@ -3290,11 +3738,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt16(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagInt16: @@ -3310,6 +3760,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt16(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagInt32: @@ -3322,11 +3773,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt32(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagInt32: @@ -3342,6 +3795,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt32(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagInt64: @@ -3354,11 +3808,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt64(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagInt64: @@ -3374,6 +3830,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt64(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagUint: @@ -3386,11 +3843,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagUint: @@ -3406,6 +3865,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagUint8: @@ -3418,11 +3878,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint8(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagUint8: @@ -3438,6 +3900,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint8(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagUint16: @@ -3450,11 +3913,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint16(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagUint16: @@ -3470,6 +3935,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint16(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagUint32: @@ -3482,11 +3948,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint32(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagUint32: @@ -3502,6 +3970,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint32(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagUint64: @@ -3514,11 +3983,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint64(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagUint64: @@ -3534,6 +4005,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint64(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagFloat32: @@ -3546,11 +4018,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToFloat32(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagFloat32: @@ -3566,6 +4040,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToFloat32(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagFloat64: @@ -3578,6 +4053,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -3590,6 +4066,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeString(fmt.Sprint(v)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagFloat64: @@ -3612,6 +4089,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeString(fmt.Sprint(v)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagString: @@ -3624,6 +4102,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -3637,6 +4116,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { enc.encodeNoEscapedString(s) } e.encodeString(string(enc.buf)) + e.encodeByte(',') enc.release() code = code.next } @@ -3653,6 +4133,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(strconv.Quote(e.ptrToString(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagBool: @@ -3665,11 +4146,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToBool(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagBool: @@ -3685,6 +4168,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToBool(ptr + code.offset))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagBytes: @@ -3697,11 +4181,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') e.encodeKey(code) e.encodeByteSlice(e.ptrToBytes(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagBytes: @@ -3717,6 +4203,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeKey(code) e.encodeByteSlice(e.ptrToBytes(ptr + code.offset)) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagMarshalJSON: @@ -3729,6 +4216,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -3751,6 +4239,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeBytes([]byte{'"', '"'}) + e.encodeByte(',') code = code.nextField } else { var buf bytes.Buffer @@ -3758,6 +4247,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { return err } e.encodeString(buf.String()) + e.encodeByte(',') code = code.next } } @@ -3791,6 +4281,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeBytes([]byte{'"', '"'}) + e.encodeByte(',') code = code.nextField } else { var buf bytes.Buffer @@ -3799,6 +4290,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeString(buf.String()) + e.encodeByte(',') code = code.next } } @@ -3812,6 +4304,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.idx) if ptr == 0 { e.encodeNull() + e.encodeByte(',') code = code.end.next } else { e.encodeByte('{') @@ -3826,6 +4319,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') code = code.next } case opStructFieldPtrAnonymousHeadStringTagMarshalText: @@ -3850,6 +4344,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } e.encodeKey(code) e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') code = code.next } case opStructFieldPtrHeadStringTagIndent: @@ -3863,6 +4358,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -3884,6 +4380,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -3891,6 +4388,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagInt8Indent: @@ -3904,6 +4402,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -3911,6 +4410,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt8(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagInt16Indent: @@ -3924,6 +4424,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -3931,6 +4432,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt16(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagInt32Indent: @@ -3944,6 +4446,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -3951,6 +4454,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt32(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagInt64Indent: @@ -3964,6 +4468,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -3971,6 +4476,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt64(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagUintIndent: @@ -3984,6 +4490,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -3991,6 +4498,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagUint8Indent: @@ -4004,6 +4512,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -4011,6 +4520,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint8(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagUint16Indent: @@ -4024,6 +4534,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -4031,6 +4542,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint16(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagUint32Indent: @@ -4044,6 +4556,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -4051,6 +4564,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint32(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagUint64Indent: @@ -4064,6 +4578,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -4071,6 +4586,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint64(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagFloat32Indent: @@ -4084,6 +4600,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -4091,6 +4608,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToFloat32(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagFloat64Indent: @@ -4104,6 +4622,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -4118,6 +4637,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(v)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagStringIndent: @@ -4131,13 +4651,23 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) e.encodeIndent(code.indent + 1) e.encodeKey(code) e.encodeByte(' ') - e.encodeString(strconv.Quote(e.ptrToString(ptr + code.offset))) + var buf bytes.Buffer + enc := NewEncoder(&buf) + s := e.ptrToString(ptr + code.offset) + if e.enabledHTMLEscape { + enc.encodeEscapedString(s) + } else { + enc.encodeNoEscapedString(s) + } + e.encodeString(string(enc.buf)) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagBoolIndent: @@ -4151,6 +4681,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -4158,6 +4689,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToBool(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructFieldPtrHeadStringTagBytesIndent: @@ -4171,6 +4703,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr == 0 { e.encodeIndent(code.indent) e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.end.next } else { e.encodeBytes([]byte{'{', '\n'}) @@ -4183,12 +4716,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeByte('"') e.encodeBytes(*(*[]byte)(unsafe.Pointer(&s))) e.encodeByte('"') + e.encodeBytes([]byte{',', '\n'}) code = code.next } case opStructField: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } if !code.anonymousKey { e.encodeKey(code) } @@ -4196,9 +4727,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { code = code.next store(ctxptr, code.idx, ptr) case opStructFieldPtrInt: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4207,19 +4735,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeInt(e.ptrToInt(p)) } + e.encodeByte(',') code = code.next case opStructFieldInt: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeInt(e.ptrToInt(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrInt8: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4228,19 +4752,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeInt8(e.ptrToInt8(p)) } + e.encodeByte(',') code = code.next case opStructFieldInt8: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeInt8(e.ptrToInt8(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrInt16: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4249,19 +4769,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeInt16(e.ptrToInt16(p)) } + e.encodeByte(',') code = code.next case opStructFieldInt16: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeInt16(e.ptrToInt16(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrInt32: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4270,19 +4786,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeInt32(e.ptrToInt32(p)) } + e.encodeByte(',') code = code.next case opStructFieldInt32: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeInt32(e.ptrToInt32(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrInt64: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4291,19 +4803,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeInt64(e.ptrToInt64(p)) } + e.encodeByte(',') code = code.next case opStructFieldInt64: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeInt64(e.ptrToInt64(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrUint: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4312,19 +4820,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeUint(e.ptrToUint(p)) } + e.encodeByte(',') code = code.next case opStructFieldUint: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeUint(e.ptrToUint(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrUint8: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4333,19 +4837,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeUint8(e.ptrToUint8(p)) } + e.encodeByte(',') code = code.next case opStructFieldUint8: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeUint8(e.ptrToUint8(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrUint16: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4354,19 +4854,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeUint16(e.ptrToUint16(p)) } + e.encodeByte(',') code = code.next case opStructFieldUint16: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeUint16(e.ptrToUint16(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrUint32: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4375,19 +4871,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeUint32(e.ptrToUint32(p)) } + e.encodeByte(',') code = code.next case opStructFieldUint32: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeUint32(e.ptrToUint32(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrUint64: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4396,19 +4888,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeUint64(e.ptrToUint64(p)) } + e.encodeByte(',') code = code.next case opStructFieldUint64: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeUint64(e.ptrToUint64(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrFloat32: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4417,24 +4905,21 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeFloat32(e.ptrToFloat32(p)) } + e.encodeByte(',') code = code.next case opStructFieldFloat32: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeFloat32(e.ptrToFloat32(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrFloat64: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) if p == 0 { e.encodeNull() + e.encodeByte(',') code = code.next break } @@ -4446,11 +4931,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeFloat64(v) + e.encodeByte(',') code = code.next case opStructFieldFloat64: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) v := e.ptrToFloat64(ptr + code.offset) @@ -4461,11 +4944,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeFloat64(v) + e.encodeByte(',') code = code.next case opStructFieldPtrString: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4474,19 +4955,15 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeString(e.ptrToString(p)) } + e.encodeByte(',') code = code.next case opStructFieldString: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeString(e.ptrToString(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldPtrBool: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := e.ptrToPtr(ptr + code.offset) @@ -4495,27 +4972,21 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { e.encodeBool(e.ptrToBool(p)) } + e.encodeByte(',') code = code.next case opStructFieldBool: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeBool(e.ptrToBool(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldBytes: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) e.encodeByteSlice(e.ptrToBytes(ptr + code.offset)) + e.encodeByte(',') code = code.next case opStructFieldMarshalJSON: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) p := ptr + code.offset @@ -4535,11 +5006,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { return err } e.encodeBytes(buf.Bytes()) + e.encodeByte(',') code = code.next case opStructFieldMarshalText: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } ptr := load(ctxptr, code.headIdx) e.encodeKey(code) p := ptr + code.offset @@ -4555,56 +5024,39 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') code = code.next case opStructFieldArray: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := ptr + code.offset code = code.next store(ctxptr, code.idx, p) case opStructFieldSlice: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := ptr + code.offset code = code.next store(ctxptr, code.idx, p) case opStructFieldMap: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := ptr + code.offset code = code.next store(ctxptr, code.idx, p) case opStructFieldMapLoad: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := ptr + code.offset code = code.next store(ctxptr, code.idx, p) case opStructFieldStruct: - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) ptr := load(ctxptr, code.headIdx) p := ptr + code.offset code = code.next store(ctxptr, code.idx, p) case opStructFieldIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -4613,119 +5065,94 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { code = code.next store(ctxptr, code.idx, p) case opStructFieldIntIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeInt(e.ptrToInt(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldInt8Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeInt8(e.ptrToInt8(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldInt16Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeInt16(e.ptrToInt16(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldInt32Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeInt32(e.ptrToInt32(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldInt64Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeInt64(e.ptrToInt64(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldUintIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeUint(e.ptrToUint(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldUint8Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeUint8(e.ptrToUint8(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldUint16Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeUint16(e.ptrToUint16(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldUint32Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeUint32(e.ptrToUint32(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldUint64Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeUint64(e.ptrToUint64(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldFloat32Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeFloat32(e.ptrToFloat32(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldFloat64Indent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -4738,31 +5165,25 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeFloat64(v) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeString(e.ptrToString(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldBoolIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') ptr := load(ctxptr, code.headIdx) e.encodeBool(e.ptrToBool(ptr + code.offset)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldBytesIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -4771,11 +5192,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeByte('"') e.encodeBytes(*(*[]byte)(unsafe.Pointer(&s))) e.encodeByte('"') + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldMarshalJSONIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeByte(',') - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -4797,11 +5216,9 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { return err } e.encodeBytes(buf.Bytes()) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldArrayIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -4810,14 +5227,12 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { header := (*reflect.SliceHeader)(unsafe.Pointer(p)) if p == 0 || header.Data == 0 { e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.nextField } else { code = code.next } case opStructFieldSliceIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -4826,14 +5241,12 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { header := (*reflect.SliceHeader)(unsafe.Pointer(p)) if p == 0 || header.Data == 0 { e.encodeNull() + e.encodeBytes([]byte{',', '\n'}) code = code.nextField } else { code = code.next } case opStructFieldMapIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -4845,7 +5258,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } else { mlen := maplen(unsafe.Pointer(p)) if mlen == 0 { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) mapCode := code.next code = mapCode.end.next } else { @@ -4853,9 +5266,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } case opStructFieldMapLoadIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -4868,29 +5278,26 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { p = uintptr(*(*unsafe.Pointer)(unsafe.Pointer(p))) mlen := maplen(unsafe.Pointer(p)) if mlen == 0 { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) code = code.nextField } else { code = code.next } } case opStructFieldStructIndent: - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } ptr := load(ctxptr, code.headIdx) p := ptr + code.offset e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') if p == 0 { - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) code = code.nextField } else { headCode := code.next if headCode.next == headCode.end { // not exists fields - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) code = code.nextField } else { code = code.next @@ -4903,9 +5310,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 || *(*uintptr)(unsafe.Pointer(p)) == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) code = code.next store(ctxptr, code.idx, p) @@ -4914,121 +5318,99 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.headIdx) v := e.ptrToInt(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeInt(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyInt8: ptr := load(ctxptr, code.headIdx) v := e.ptrToInt8(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeInt8(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyInt16: ptr := load(ctxptr, code.headIdx) v := e.ptrToInt16(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeInt16(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyInt32: ptr := load(ctxptr, code.headIdx) v := e.ptrToInt32(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeInt32(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyInt64: ptr := load(ctxptr, code.headIdx) v := e.ptrToInt64(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeInt64(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyUint: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeUint(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyUint8: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint8(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeUint8(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyUint16: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint16(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeUint16(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyUint32: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint32(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeUint32(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyUint64: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint64(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeUint64(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyFloat32: ptr := load(ctxptr, code.headIdx) v := e.ptrToFloat32(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeFloat32(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyFloat64: @@ -5041,44 +5423,36 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { Str: strconv.FormatFloat(v, 'g', -1, 64), } } - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeFloat64(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyString: ptr := load(ctxptr, code.headIdx) v := e.ptrToString(ptr + code.offset) if v != "" { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyBool: ptr := load(ctxptr, code.headIdx) v := e.ptrToBool(ptr + code.offset) if v { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeBool(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyBytes: ptr := load(ctxptr, code.headIdx) v := e.ptrToBytes(ptr + code.offset) if len(v) > 0 { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeByteSlice(v) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyMarshalJSON: @@ -5101,6 +5475,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { return err } e.encodeBytes(buf.Bytes()) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyMarshalText: @@ -5123,6 +5498,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') } code = code.next case opStructFieldOmitEmptyArray: @@ -5132,9 +5508,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 || header.Data == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } code = code.next } case opStructFieldOmitEmptySlice: @@ -5144,9 +5517,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 || header.Data == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } code = code.next } case opStructFieldOmitEmptyMap: @@ -5159,9 +5529,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if mlen == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } code = code.next } } @@ -5176,9 +5543,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if mlen == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } code = code.next } } @@ -5188,9 +5552,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 || *(*uintptr)(unsafe.Pointer(p)) == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5201,143 +5562,121 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { ptr := load(ctxptr, code.headIdx) v := e.ptrToInt(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeInt(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyInt8Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToInt8(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeInt8(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyInt16Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToInt16(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeInt16(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyInt32Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToInt32(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeInt32(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyInt64Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToInt64(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeInt64(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyUintIndent: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeUint(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyUint8Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint8(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeUint8(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyUint16Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint16(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeUint16(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyUint32Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint32(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeUint32(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyUint64Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToUint64(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeUint64(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyFloat32Indent: ptr := load(ctxptr, code.headIdx) v := e.ptrToFloat32(ptr + code.offset) if v != 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeFloat32(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyFloat64Indent: @@ -5350,48 +5689,39 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { Str: strconv.FormatFloat(v, 'g', -1, 64), } } - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeFloat64(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyStringIndent: ptr := load(ctxptr, code.headIdx) v := e.ptrToString(ptr + code.offset) if v != "" { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyBoolIndent: ptr := load(ctxptr, code.headIdx) v := e.ptrToBool(ptr + code.offset) if v { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeBool(v) + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyBytesIndent: ptr := load(ctxptr, code.headIdx) v := e.ptrToBytes(ptr + code.offset) if len(v) > 0 { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5399,6 +5729,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeByte('"') e.encodeBytes(*(*[]byte)(unsafe.Pointer(&s))) e.encodeByte('"') + e.encodeBytes([]byte{',', '\n'}) } code = code.next case opStructFieldOmitEmptyArrayIndent: @@ -5408,9 +5739,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 || header.Data == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5423,9 +5751,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 || header.Data == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5441,9 +5766,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if mlen == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5461,9 +5783,6 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if mlen == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5476,16 +5795,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if p == 0 { code = code.nextField } else { - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') headCode := code.next if headCode.next == headCode.end { // not exists fields - e.encodeBytes([]byte{'{', '}'}) + e.encodeBytes([]byte{'{', '}', ',', '\n'}) code = code.nextField } else { code = code.next @@ -5495,99 +5811,74 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { case opStructFieldStringTag: ptr := load(ctxptr, code.headIdx) p := ptr + code.offset - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) code = code.next store(ctxptr, code.idx, p) case opStructFieldStringTagInt: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagInt8: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt8(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagInt16: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt16(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagInt32: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt32(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagInt64: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToInt64(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagUint: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagUint8: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint8(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagUint16: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint16(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagUint32: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint32(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagUint64: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToUint64(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagFloat32: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToFloat32(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagFloat64: ptr := load(ctxptr, code.headIdx) @@ -5598,36 +5889,30 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { Str: strconv.FormatFloat(v, 'g', -1, 64), } } - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(v)) + e.encodeByte(',') code = code.next case opStructFieldStringTagString: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) - e.encodeString(strconv.Quote(e.ptrToString(ptr + code.offset))) + var b bytes.Buffer + enc := NewEncoder(&b) + enc.encodeString(e.ptrToString(ptr + code.offset)) + e.encodeString(string(enc.buf)) code = code.next case opStructFieldStringTagBool: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeString(fmt.Sprint(e.ptrToBool(ptr + code.offset))) + e.encodeByte(',') code = code.next case opStructFieldStringTagBytes: ptr := load(ctxptr, code.headIdx) v := e.ptrToBytes(ptr + code.offset) - if e.buf[len(e.buf)-1] != '{' { - e.encodeByte(',') - } e.encodeKey(code) e.encodeByteSlice(v) + e.encodeByte(',') code = code.next case opStructFieldStringTagMarshalJSON: ptr := load(ctxptr, code.headIdx) @@ -5648,6 +5933,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { return err } e.encodeString(buf.String()) + e.encodeByte(',') code = code.next case opStructFieldStringTagMarshalText: ptr := load(ctxptr, code.headIdx) @@ -5664,13 +5950,11 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeByte(',') code = code.next case opStructFieldStringTagIndent: ptr := load(ctxptr, code.headIdx) p := ptr + code.offset - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5678,113 +5962,91 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { store(ctxptr, code.idx, p) case opStructFieldStringTagIntIndent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagInt8Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt8(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagInt16Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt16(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagInt32Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt32(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagInt64Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToInt64(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagUintIndent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagUint8Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint8(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagUint16Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint16(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagUint32Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint32(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagUint64Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToUint64(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagFloat32Indent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToFloat32(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagFloat64Indent: ptr := load(ctxptr, code.headIdx) @@ -5795,19 +6057,14 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { Str: strconv.FormatFloat(v, 'g', -1, 64), } } - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(v)) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagStringIndent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5815,23 +6072,19 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { enc := NewEncoder(&b) enc.encodeString(e.ptrToString(ptr + code.offset)) e.encodeString(string(enc.buf)) + e.encodeBytes([]byte{',', '\n'}) enc.release() code = code.next case opStructFieldStringTagBoolIndent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') e.encodeString(fmt.Sprint(e.ptrToBool(ptr + code.offset))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagBytesIndent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5841,12 +6094,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { e.encodeByte('"') e.encodeBytes(*(*[]byte)(unsafe.Pointer(&s))) e.encodeByte('"') + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagMarshalJSONIndent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5867,12 +6118,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { return err } e.encodeString(buf.String()) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructFieldStringTagMarshalTextIndent: ptr := load(ctxptr, code.headIdx) - if e.buf[len(e.buf)-2] != '{' || e.buf[len(e.buf)-1] == '}' { - e.encodeBytes([]byte{',', '\n'}) - } e.encodeIndent(code.indent) e.encodeKey(code) e.encodeByte(' ') @@ -5889,16 +6138,29 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { } } e.encodeString(*(*string)(unsafe.Pointer(&bytes))) + e.encodeBytes([]byte{',', '\n'}) code = code.next case opStructEnd: - e.encodeByte('}') + last := len(e.buf) - 1 + if e.buf[last] == ',' { + e.buf[last] = '}' + } else { + e.encodeByte('}') + } + e.encodeByte(',') code = code.next case opStructAnonymousEnd: code = code.next case opStructEndIndent: + last := len(e.buf) - 1 + if e.buf[last] == '\n' { + // to remove ',' and '\n' characters + e.buf = e.buf[:len(e.buf)-2] + } e.encodeByte('\n') e.encodeIndent(code.indent) e.encodeByte('}') + e.encodeBytes([]byte{',', '\n'}) code = code.next case opEnd: goto END diff --git a/json.go b/json.go index b25f191..ad010b9 100644 --- a/json.go +++ b/json.go @@ -391,7 +391,7 @@ func HTMLEscape(dst *bytes.Buffer, src []byte) { enc := NewEncoder(dst) enc.SetEscapeHTML(true) enc.encode(v) - dst.Write(enc.buf) + dst.Write(enc.buf[:len(enc.buf)-1]) // remove last ',' character } // Valid reports whether data is a valid JSON encoding.