diff --git a/encode_test.go b/encode_test.go index 98f71b0..83075c5 100644 --- a/encode_test.go +++ b/encode_test.go @@ -2086,3 +2086,25 @@ func TestIssue263(t *testing.T) { t.Fatalf("expected:[%s] but got:[%s]", string(expected), string(actual)) } } + +func TestEmbeddedNotFirstField(t *testing.T) { + type Embedded struct { + Has bool `json:"has"` + } + type T struct { + X int `json:"is"` + Embedded `json:"child"` + } + p := T{X: 10, Embedded: Embedded{Has: true}} + expected, err := stdjson.Marshal(&p) + if err != nil { + t.Fatal(err) + } + got, err := json.Marshal(&p) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(expected, got) { + t.Fatalf("failed to encode embedded structure. expected = %q but got %q", expected, got) + } +} diff --git a/internal/cmd/generator/vm.go.tmpl b/internal/cmd/generator/vm.go.tmpl index f2c992a..5c62354 100644 --- a/internal/cmd/generator/vm.go.tmpl +++ b/internal/cmd/generator/vm.go.tmpl @@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next } case encoder.OpStructField: - if code.Flags&encoder.AnonymousKeyFlags == 0 { + if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 { b = appendStructKey(ctx, code, b) } p := load(ctxptr, code.Idx) + uintptr(code.Offset) diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index f2c992a..5c62354 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next } case encoder.OpStructField: - if code.Flags&encoder.AnonymousKeyFlags == 0 { + if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 { b = appendStructKey(ctx, code, b) } p := load(ctxptr, code.Idx) + uintptr(code.Offset) diff --git a/internal/encoder/vm_color/vm.go b/internal/encoder/vm_color/vm.go index 70de3e7..71aa3f7 100644 --- a/internal/encoder/vm_color/vm.go +++ b/internal/encoder/vm_color/vm.go @@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next } case encoder.OpStructField: - if code.Flags&encoder.AnonymousKeyFlags == 0 { + if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 { b = appendStructKey(ctx, code, b) } p := load(ctxptr, code.Idx) + uintptr(code.Offset) diff --git a/internal/encoder/vm_color_indent/vm.go b/internal/encoder/vm_color_indent/vm.go index 2d014e5..60d0083 100644 --- a/internal/encoder/vm_color_indent/vm.go +++ b/internal/encoder/vm_color_indent/vm.go @@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next } case encoder.OpStructField: - if code.Flags&encoder.AnonymousKeyFlags == 0 { + if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 { b = appendStructKey(ctx, code, b) } p := load(ctxptr, code.Idx) + uintptr(code.Offset) diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index 25d2ebc..e817484 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next } case encoder.OpStructField: - if code.Flags&encoder.AnonymousKeyFlags == 0 { + if code.Flags&encoder.IsTaggedKeyFlags != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 { b = appendStructKey(ctx, code, b) } p := load(ctxptr, code.Idx) + uintptr(code.Offset)