mirror of https://github.com/goccy/go-json.git
Fix encoding of embedded struct that isn't first field
This commit is contained in:
parent
a1780c18a6
commit
104829e78f
|
@ -2086,3 +2086,25 @@ func TestIssue263(t *testing.T) {
|
||||||
t.Fatalf("expected:[%s] but got:[%s]", string(expected), string(actual))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
code = code.Next
|
code = code.Next
|
||||||
}
|
}
|
||||||
case encoder.OpStructField:
|
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)
|
b = appendStructKey(ctx, code, b)
|
||||||
}
|
}
|
||||||
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
||||||
|
|
|
@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
code = code.Next
|
code = code.Next
|
||||||
}
|
}
|
||||||
case encoder.OpStructField:
|
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)
|
b = appendStructKey(ctx, code, b)
|
||||||
}
|
}
|
||||||
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
||||||
|
|
|
@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
code = code.Next
|
code = code.Next
|
||||||
}
|
}
|
||||||
case encoder.OpStructField:
|
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)
|
b = appendStructKey(ctx, code, b)
|
||||||
}
|
}
|
||||||
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
||||||
|
|
|
@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
code = code.Next
|
code = code.Next
|
||||||
}
|
}
|
||||||
case encoder.OpStructField:
|
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)
|
b = appendStructKey(ctx, code, b)
|
||||||
}
|
}
|
||||||
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
||||||
|
|
|
@ -3439,7 +3439,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
code = code.Next
|
code = code.Next
|
||||||
}
|
}
|
||||||
case encoder.OpStructField:
|
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)
|
b = appendStructKey(ctx, code, b)
|
||||||
}
|
}
|
||||||
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
p := load(ctxptr, code.Idx) + uintptr(code.Offset)
|
||||||
|
|
Loading…
Reference in New Issue