Skip to content

Commit

Permalink
Merge branch 'master' into fix/goccy#374
Browse files Browse the repository at this point in the history
  • Loading branch information
orisano committed Jul 7, 2022
2 parents 8f5055b + 88aa13e commit 79d8df0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
17 changes: 17 additions & 0 deletions decode_test.go
Expand Up @@ -3968,3 +3968,20 @@ func TestIssue335(t *testing.T) {
t.Errorf("unexpected success")
}
}

func TestIssue372(t *testing.T) {
type A int
type T struct {
_ int
*A
}
var v T
err := json.Unmarshal([]byte(`{"A":7}`), &v)
assertErr(t, err)

got := *v.A
expected := A(7)
if got != expected {
t.Errorf("unexpected result: %v != %v", got, expected)
}
}
29 changes: 29 additions & 0 deletions encode_test.go
Expand Up @@ -2427,6 +2427,35 @@ func TestIssue376(t *testing.T) {
}
}

type Issue370 struct {
String string
Valid bool
}

func (i *Issue370) MarshalJSON() ([]byte, error) {
if !i.Valid {
return json.Marshal(nil)
}
return json.Marshal(i.String)
}

func TestIssue370(t *testing.T) {
v := []struct {
V Issue370
}{
{V: Issue370{String: "test", Valid: true}},
}
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
got := string(b)
expected := `[{"V":"test"}]`
if got != expected {
t.Errorf("unexpected result: %v != %v", got, expected)
}
}

func TestIssue374(t *testing.T) {
r := io.MultiReader(strings.NewReader(strings.Repeat(" ", 505)+`"\u`), strings.NewReader(`0000"`))
var v interface{}
Expand Down
9 changes: 9 additions & 0 deletions internal/decoder/compile.go
Expand Up @@ -393,6 +393,15 @@ func compileStruct(typ *runtime.Type, structName, fieldName string, structTypeTo
}
allFields = append(allFields, fieldSet)
}
} else {
fieldSet := &structFieldSet{
dec: pdec,
offset: field.Offset,
isTaggedKey: tag.IsTaggedKey,
key: field.Name,
keyLen: int64(len(field.Name)),
}
allFields = append(allFields, fieldSet)
}
} else {
fieldSet := &structFieldSet{
Expand Down
5 changes: 4 additions & 1 deletion internal/encoder/compiler.go
Expand Up @@ -487,7 +487,10 @@ func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) {
case typ.Kind() == reflect.Map:
return c.ptrCode(runtime.PtrTo(typ))
default:
code, err := c.typeToCodeWithPtr(typ, false)
// isPtr was originally used to indicate whether the type of top level is pointer.
// However, since the slice/array element is a specification that can get the pointer address, explicitly set isPtr to true.
// See here for related issues: https://github.com/goccy/go-json/issues/370
code, err := c.typeToCodeWithPtr(typ, true)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 79d8df0

Please sign in to comment.