Add test case

This commit is contained in:
Masaaki Goshima 2021-03-11 19:07:52 +09:00
parent be35ec63b8
commit 6c5b9420d7
2 changed files with 90 additions and 1 deletions

View File

@ -1302,10 +1302,16 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
anonymousFields[k] = append(anonymousFields[k], v...)
}
valueCode.decIndent()
// fix issue144
if !(isPtr && strings.Contains(valueCode.op.String(), "Marshal")) {
valueCode.indirect = indirect
}
} else {
valueCode.indirect = indirect
}
key := fmt.Sprintf(`"%s":`, tag.key)
escapedKey := fmt.Sprintf(`%s:`, string(encodeEscapedString([]byte{}, tag.key)))
valueCode.indirect = indirect
fieldCode := &opcode{
typ: valueCode.typ,
displayIdx: fieldOpcodeIndex,

View File

@ -1706,3 +1706,86 @@ func TestIssue147(t *testing.T) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}
type testIssue144 struct {
name string
number int64
}
func (v *testIssue144) MarshalJSON() ([]byte, error) {
if v.name != "" {
return json.Marshal(v.name)
}
return json.Marshal(v.number)
}
func TestIssue144(t *testing.T) {
type Embeded struct {
Field *testIssue144 `json:"field,omitempty"`
}
type T struct {
Embeded
}
{
v := T{
Embeded: Embeded{Field: &testIssue144{name: "hoge"}},
}
got, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.Marshal(v)
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}
{
v := &T{
Embeded: Embeded{Field: &testIssue144{name: "hoge"}},
}
got, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.Marshal(v)
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}
}
func TestIssue118(t *testing.T) {
type data struct {
Columns []string `json:"columns"`
Rows1 [][]string `json:"rows1"`
Rows2 [][]string `json:"rows2"`
}
v := data{Columns: []string{"1", "2", "3"}}
got, err := json.MarshalIndent(v, "", " ")
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.MarshalIndent(v, "", " ")
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}
func TestIssue104(t *testing.T) {
type A struct {
Field1 string
Field2 int
Field3 float64
}
type T struct {
Field A
}
got, err := json.Marshal(T{})
if err != nil {
t.Fatal(err)
}
expect, _ := stdjson.Marshal(T{})
if !bytes.Equal(expect, got) {
t.Fatalf("expect %q but got %q", string(expect), string(got))
}
}