mirror of https://github.com/goccy/go-json.git
Fix encoding of slice type
This commit is contained in:
parent
ae68eaf96b
commit
52b2bf1089
|
@ -6,6 +6,7 @@ import (
|
|||
)
|
||||
|
||||
func intptr(v int) *int { return &v }
|
||||
func intptr3(v int) ***int { vv := &v; vvv := &vv; return &vvv }
|
||||
func int8ptr(v int8) *int8 { return &v }
|
||||
func int16ptr(v int16) *int16 { return &v }
|
||||
func int32ptr(v int32) *int32 { return &v }
|
||||
|
|
|
@ -28,10 +28,81 @@ func TestCoverInt(t *testing.T) {
|
|||
A *int `json:"a,string"`
|
||||
}
|
||||
|
||||
type structIntTriplePtr struct {
|
||||
A ***int `json:"a"`
|
||||
}
|
||||
type structIntTriplePtrOmitEmpty struct {
|
||||
A ***int `json:"a,omitempty"`
|
||||
}
|
||||
type structIntTriplePtrString struct {
|
||||
A ***int `json:"a,string"`
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
data interface{}
|
||||
}{
|
||||
{
|
||||
name: "Int",
|
||||
data: 10,
|
||||
},
|
||||
{
|
||||
name: "IntPtr",
|
||||
data: intptr(10),
|
||||
},
|
||||
{
|
||||
name: "IntTriplePtr",
|
||||
data: intptr3(10),
|
||||
},
|
||||
{
|
||||
name: "IntSlice",
|
||||
data: []int{1, 2, 3, 4, 5},
|
||||
},
|
||||
{
|
||||
name: "IntPtrSlice",
|
||||
data: []*int{nil, nil, intptr(1), nil, nil},
|
||||
},
|
||||
{
|
||||
name: "StructIntSliceNil",
|
||||
data: ([]structInt)(nil),
|
||||
},
|
||||
{
|
||||
name: "PtrStructIntSliceNil",
|
||||
data: ([]*structInt)(nil),
|
||||
},
|
||||
{
|
||||
name: "StructIntSliceZero",
|
||||
data: []structInt{},
|
||||
},
|
||||
{
|
||||
name: "PtrStructIntSliceZero",
|
||||
data: []*structInt{},
|
||||
},
|
||||
{
|
||||
name: "PtrStructIntNilSlice",
|
||||
data: []*structInt{nil, nil},
|
||||
},
|
||||
{
|
||||
name: "StructIntOmitEmptySliceNil",
|
||||
data: ([]structIntOmitEmpty)(nil),
|
||||
},
|
||||
{
|
||||
name: "PtrStructIntOmitEmptySliceNil",
|
||||
data: ([]*structIntOmitEmpty)(nil),
|
||||
},
|
||||
{
|
||||
name: "StructIntOmitEmptySliceZero",
|
||||
data: []structIntOmitEmpty{},
|
||||
},
|
||||
{
|
||||
name: "PtrStructIntOmitEmptySliceZero",
|
||||
data: []*structIntOmitEmpty{},
|
||||
},
|
||||
{
|
||||
name: "PtrStructIntOmitEmptyNilSlice",
|
||||
data: []*structIntOmitEmpty{nil, nil},
|
||||
},
|
||||
|
||||
// HeadIntZero
|
||||
{
|
||||
name: "HeadIntZero",
|
||||
|
|
|
@ -390,7 +390,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
case opSliceHead:
|
||||
p := load(ctxptr, code.idx)
|
||||
slice := ptrToSlice(p)
|
||||
if p == 0 || uintptr(slice.data) == 0 {
|
||||
if p == 0 || slice.data == nil {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
|
|
|
@ -332,7 +332,7 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o
|
|||
case opSliceHead:
|
||||
p := load(ctxptr, code.idx)
|
||||
slice := ptrToSlice(p)
|
||||
if p == 0 || uintptr(slice.data) == 0 {
|
||||
if p == 0 || slice.data == nil {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
|
|
|
@ -348,13 +348,13 @@ func encodeRunEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
|
|||
code = code.next
|
||||
case opSliceHead:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
slice := ptrToSlice(p)
|
||||
if p == 0 || slice.data == nil {
|
||||
b = appendIndent(ctx, b, code.indent)
|
||||
b = encodeNull(b)
|
||||
b = encodeIndentComma(b)
|
||||
code = code.end.next
|
||||
} else {
|
||||
slice := ptrToSlice(p)
|
||||
store(ctxptr, code.elemIdx, 0)
|
||||
store(ctxptr, code.length, uintptr(slice.len))
|
||||
store(ctxptr, code.idx, uintptr(slice.data))
|
||||
|
@ -365,7 +365,7 @@ func encodeRunEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
|
|||
store(ctxptr, code.idx, uintptr(slice.data))
|
||||
} else {
|
||||
b = appendIndent(ctx, b, code.indent)
|
||||
b = append(b, '[', ']', '\n')
|
||||
b = append(b, '[', ']', ',', '\n')
|
||||
code = code.end.next
|
||||
}
|
||||
}
|
||||
|
|
|
@ -348,13 +348,13 @@ func encodeRunIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, op
|
|||
code = code.next
|
||||
case opSliceHead:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
slice := ptrToSlice(p)
|
||||
if p == 0 || slice.data == nil {
|
||||
b = appendIndent(ctx, b, code.indent)
|
||||
b = encodeNull(b)
|
||||
b = encodeIndentComma(b)
|
||||
code = code.end.next
|
||||
} else {
|
||||
slice := ptrToSlice(p)
|
||||
store(ctxptr, code.elemIdx, 0)
|
||||
store(ctxptr, code.length, uintptr(slice.len))
|
||||
store(ctxptr, code.idx, uintptr(slice.data))
|
||||
|
@ -365,7 +365,7 @@ func encodeRunIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, op
|
|||
store(ctxptr, code.idx, uintptr(slice.data))
|
||||
} else {
|
||||
b = appendIndent(ctx, b, code.indent)
|
||||
b = append(b, '[', ']', '\n')
|
||||
b = append(b, '[', ']', ',', '\n')
|
||||
code = code.end.next
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue