mirror of https://github.com/goccy/go-json.git
Fix encode issue for embed struct with tag
This commit is contained in:
parent
85a5597342
commit
ac9a7dd8e3
|
@ -191,6 +191,59 @@ func Test_Marshal(t *testing.T) {
|
|||
})
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("embedded with tag", func(t *testing.T) {
|
||||
type T struct {
|
||||
A string `json:"a"`
|
||||
}
|
||||
type U struct {
|
||||
*T `json:"t"`
|
||||
B string `json:"b"`
|
||||
}
|
||||
type T2 struct {
|
||||
A string `json:"a,omitempty"`
|
||||
}
|
||||
type U2 struct {
|
||||
*T2 `json:"t,omitempty"`
|
||||
B string `json:"b,omitempty"`
|
||||
}
|
||||
t.Run("exists field", func(t *testing.T) {
|
||||
bytes, err := json.Marshal(&U{
|
||||
T: &T{
|
||||
A: "aaa",
|
||||
},
|
||||
B: "bbb",
|
||||
})
|
||||
assertErr(t, err)
|
||||
assertEq(t, "embedded", `{"t":{"a":"aaa"},"b":"bbb"}`, string(bytes))
|
||||
t.Run("omitempty", func(t *testing.T) {
|
||||
bytes, err := json.Marshal(&U2{
|
||||
T2: &T2{
|
||||
A: "aaa",
|
||||
},
|
||||
B: "bbb",
|
||||
})
|
||||
assertErr(t, err)
|
||||
assertEq(t, "embedded", `{"t":{"a":"aaa"},"b":"bbb"}`, string(bytes))
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("none field", func(t *testing.T) {
|
||||
bytes, err := json.Marshal(&U{
|
||||
B: "bbb",
|
||||
})
|
||||
assertErr(t, err)
|
||||
assertEq(t, "embedded", `{"t":null,"b":"bbb"}`, string(bytes))
|
||||
t.Run("omitempty", func(t *testing.T) {
|
||||
bytes, err := json.Marshal(&U2{
|
||||
B: "bbb",
|
||||
})
|
||||
assertErr(t, err)
|
||||
assertEq(t, "embedded", `{"b":"bbb"}`, string(bytes))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("omitempty", func(t *testing.T) {
|
||||
type T struct {
|
||||
A int `json:",omitempty"`
|
||||
|
|
|
@ -578,9 +578,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
if code.Flags&encoder.AnonymousHeadFlags == 0 {
|
||||
b = appendStructHead(ctx, b)
|
||||
}
|
||||
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
|
||||
if len(code.Key) > 0 {
|
||||
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
|
||||
b = appendStructKey(ctx, code, b)
|
||||
}
|
||||
}
|
||||
p += uintptr(code.Offset)
|
||||
code = code.Next
|
||||
store(ctxptr, code.Idx, p)
|
||||
|
|
|
@ -578,9 +578,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
if code.Flags&encoder.AnonymousHeadFlags == 0 {
|
||||
b = appendStructHead(ctx, b)
|
||||
}
|
||||
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
|
||||
if len(code.Key) > 0 {
|
||||
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
|
||||
b = appendStructKey(ctx, code, b)
|
||||
}
|
||||
}
|
||||
p += uintptr(code.Offset)
|
||||
code = code.Next
|
||||
store(ctxptr, code.Idx, p)
|
||||
|
|
|
@ -578,9 +578,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
if code.Flags&encoder.AnonymousHeadFlags == 0 {
|
||||
b = appendStructHead(ctx, b)
|
||||
}
|
||||
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
|
||||
if len(code.Key) > 0 {
|
||||
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
|
||||
b = appendStructKey(ctx, code, b)
|
||||
}
|
||||
}
|
||||
p += uintptr(code.Offset)
|
||||
code = code.Next
|
||||
store(ctxptr, code.Idx, p)
|
||||
|
|
|
@ -578,9 +578,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
if code.Flags&encoder.AnonymousHeadFlags == 0 {
|
||||
b = appendStructHead(ctx, b)
|
||||
}
|
||||
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
|
||||
if len(code.Key) > 0 {
|
||||
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
|
||||
b = appendStructKey(ctx, code, b)
|
||||
}
|
||||
}
|
||||
p += uintptr(code.Offset)
|
||||
code = code.Next
|
||||
store(ctxptr, code.Idx, p)
|
||||
|
|
|
@ -578,9 +578,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
|||
if code.Flags&encoder.AnonymousHeadFlags == 0 {
|
||||
b = appendStructHead(ctx, b)
|
||||
}
|
||||
if (code.Flags&encoder.AnonymousKeyFlags) == 0 && len(code.Key) > 0 {
|
||||
if len(code.Key) > 0 {
|
||||
if (code.Flags&encoder.IsTaggedKeyFlags) != 0 || code.Flags&encoder.AnonymousKeyFlags == 0 {
|
||||
b = appendStructKey(ctx, code, b)
|
||||
}
|
||||
}
|
||||
p += uintptr(code.Offset)
|
||||
code = code.Next
|
||||
store(ctxptr, code.Idx, p)
|
||||
|
|
Loading…
Reference in New Issue