Merge pull request #286 from goccy/feature/fix-empty-struct-interface

Fix encoding of empty struct interface type
This commit is contained in:
Masaaki Goshima 2021-09-01 11:57:45 +09:00 committed by GitHub
commit 3fc39932e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -2134,3 +2134,17 @@ func TestImplementedMethodInterfaceType(t *testing.T) {
t.Fatalf("failed to encode implemented method interface type. expected:[%q] but got:[%q]", expected, got)
}
}
func TestEmptyStructInterface(t *testing.T) {
expected, err := stdjson.Marshal([]interface{}{struct{}{}})
if err != nil {
t.Fatal(err)
}
got, err := json.Marshal([]interface{}{struct{}{}})
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("failed to encode empty struct interface. expected:[%q] but got:[%q]", expected, got)
}
}

View File

@ -1506,7 +1506,6 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
structEndCode := &Opcode{
Op: OpStructEnd,
Next: newEndOp(ctx),
Type: nil,
Indent: ctx.indent,
}
@ -1531,6 +1530,7 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
structEndCode.DisplayIdx = ctx.opcodeIndex
structEndCode.Idx = opcodeOffset(ctx.ptrIndex)
ctx.incIndex()
structEndCode.Next = newEndOp(ctx)
if prevField != nil && prevField.NextField == nil {
prevField.NextField = structEndCode