forked from mirror/go-json
Merge pull request #265 from peterlimg/master
Fix encode issue for embed struct with tags
This commit is contained in:
commit
a1780c18a6
|
@ -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)
|
||||
|
|
|
@ -170,6 +170,7 @@ RETRY:
|
|||
s.buf = append(s.buf[:s.cursor-1], s.buf[s.cursor:]...)
|
||||
s.length--
|
||||
s.cursor--
|
||||
p = s.bufptr()
|
||||
return p, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1417,7 +1417,7 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
|
|||
valueCode = code
|
||||
}
|
||||
|
||||
if field.Anonymous {
|
||||
if field.Anonymous && !tag.IsTaggedKey {
|
||||
tagKey := ""
|
||||
if tag.IsTaggedKey {
|
||||
tagKey = tag.Key
|
||||
|
@ -1425,6 +1425,7 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
|
|||
for k, v := range anonymousStructFieldPairMap(tags, tagKey, valueCode) {
|
||||
anonymousFields[k] = append(anonymousFields[k], v...)
|
||||
}
|
||||
|
||||
valueCode.decIndent()
|
||||
|
||||
// fix issue144
|
||||
|
|
|
@ -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