mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
Merge pull request #316 from proemergotech/master
fix #308 do NOT skip embedded structs without tag when OnlyTaggedFiel…
This commit is contained in:
commit
5bc9320502
@ -172,3 +172,58 @@ func Test_CaseSensitive_MoreThanTenFields(t *testing.T) {
|
|||||||
should.Equal(tc.expectedOutput, output)
|
should.Equal(tc.expectedOutput, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type onlyTaggedFieldStruct struct {
|
||||||
|
A string `json:"a"`
|
||||||
|
B string
|
||||||
|
FSimpl F `json:"f_simpl"`
|
||||||
|
ISimpl I
|
||||||
|
FPtr *F `json:"f_ptr"`
|
||||||
|
IPtr *I
|
||||||
|
F
|
||||||
|
*I
|
||||||
|
}
|
||||||
|
|
||||||
|
type F struct {
|
||||||
|
G string `json:"g"`
|
||||||
|
H string
|
||||||
|
}
|
||||||
|
|
||||||
|
type I struct {
|
||||||
|
J string `json:"j"`
|
||||||
|
K string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_OnlyTaggedField(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
|
||||||
|
obj := onlyTaggedFieldStruct{
|
||||||
|
A: "a",
|
||||||
|
B: "b",
|
||||||
|
FSimpl: F{G: "g", H: "h"},
|
||||||
|
ISimpl: I{J: "j", K: "k"},
|
||||||
|
FPtr: &F{G: "g", H: "h"},
|
||||||
|
IPtr: &I{J: "j", K: "k"},
|
||||||
|
F: F{G: "g", H: "h"},
|
||||||
|
I: &I{J: "j", K: "k"},
|
||||||
|
}
|
||||||
|
|
||||||
|
output, err := jsoniter.Config{OnlyTaggedField: true}.Froze().Marshal(obj)
|
||||||
|
should.Nil(err)
|
||||||
|
|
||||||
|
m := make(map[string]interface{})
|
||||||
|
err = jsoniter.Unmarshal(output, &m)
|
||||||
|
should.Nil(err)
|
||||||
|
|
||||||
|
should.Equal(map[string]interface{}{
|
||||||
|
"a": "a",
|
||||||
|
"f_simpl": map[string]interface{}{
|
||||||
|
"g": "g",
|
||||||
|
},
|
||||||
|
"f_ptr": map[string]interface{}{
|
||||||
|
"g": "g",
|
||||||
|
},
|
||||||
|
"g": "g",
|
||||||
|
"j": "j",
|
||||||
|
}, m)
|
||||||
|
}
|
||||||
|
@ -338,7 +338,7 @@ func describeStruct(ctx *ctx, typ reflect2.Type) *StructDescriptor {
|
|||||||
for i := 0; i < structType.NumField(); i++ {
|
for i := 0; i < structType.NumField(); i++ {
|
||||||
field := structType.Field(i)
|
field := structType.Field(i)
|
||||||
tag, hastag := field.Tag().Lookup(ctx.getTagKey())
|
tag, hastag := field.Tag().Lookup(ctx.getTagKey())
|
||||||
if ctx.onlyTaggedField && !hastag {
|
if ctx.onlyTaggedField && !hastag && !field.Anonymous() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tagParts := strings.Split(tag, ",")
|
tagParts := strings.Split(tag, ",")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user