Skip to content

Commit fb71671

Browse files
committed
fix: change isPtr to true on listElemCode
fix #370
1 parent 1468eef commit fb71671

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

encode_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,3 +2424,32 @@ func TestIssue376(t *testing.T) {
24242424
t.Errorf("unexpected result: %v != %v", got, expected)
24252425
}
24262426
}
2427+
2428+
type Issue370 struct {
2429+
String string
2430+
Valid bool
2431+
}
2432+
2433+
func (i *Issue370) MarshalJSON() ([]byte, error) {
2434+
if !i.Valid {
2435+
return json.Marshal(nil)
2436+
}
2437+
return json.Marshal(i.String)
2438+
}
2439+
2440+
func TestIssue370(t *testing.T) {
2441+
v := []struct {
2442+
V Issue370
2443+
}{
2444+
{V: Issue370{String: "test", Valid: true}},
2445+
}
2446+
b, err := json.Marshal(v)
2447+
if err != nil {
2448+
t.Fatal(err)
2449+
}
2450+
got := string(b)
2451+
expected := `[{"V":"test"}]`
2452+
if got != expected {
2453+
t.Errorf("unexpected result: %v != %v", got, expected)
2454+
}
2455+
}

internal/encoder/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) {
487487
case typ.Kind() == reflect.Map:
488488
return c.ptrCode(runtime.PtrTo(typ))
489489
default:
490-
code, err := c.typeToCodeWithPtr(typ, false)
490+
code, err := c.typeToCodeWithPtr(typ, true)
491491
if err != nil {
492492
return nil, err
493493
}

0 commit comments

Comments
 (0)