Merge branch 'master' into fix/#374

This commit is contained in:
Nao Yonashiro 2022-07-07 15:12:08 +09:00 committed by GitHub
commit 79d8df005a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 1 deletions

View File

@ -3968,3 +3968,20 @@ func TestIssue335(t *testing.T) {
t.Errorf("unexpected success") 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)
}
}

View File

@ -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) { func TestIssue374(t *testing.T) {
r := io.MultiReader(strings.NewReader(strings.Repeat(" ", 505)+`"\u`), strings.NewReader(`0000"`)) r := io.MultiReader(strings.NewReader(strings.Repeat(" ", 505)+`"\u`), strings.NewReader(`0000"`))
var v interface{} var v interface{}

View File

@ -393,6 +393,15 @@ func compileStruct(typ *runtime.Type, structName, fieldName string, structTypeTo
} }
allFields = append(allFields, fieldSet) 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 { } else {
fieldSet := &structFieldSet{ fieldSet := &structFieldSet{

View File

@ -487,7 +487,10 @@ func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) {
case typ.Kind() == reflect.Map: case typ.Kind() == reflect.Map:
return c.ptrCode(runtime.PtrTo(typ)) return c.ptrCode(runtime.PtrTo(typ))
default: 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 { if err != nil {
return nil, err return nil, err
} }