Repository: goccy/go-json (go)

Issue:
Serializing struct with embedded interface results in malformed json
Example:
```
type I interface {
	Foo()
}

type A struct {
	I
	Val string
}

func TestStructWithEmbeddedInterface(t *testing.T) {
	var s A
	s.Val = "456"

	j1, _ := json.Marshal(s)  // This uses encoding/json.
	fmt.Println("expected", string(j1))

	j2, _ := goccyjson.Marshal(s)  // This uses this library.
	fmt.Println("actual", string(j2))
}
```

The code above will generate a json string that is not valid.
 
Expected result (using `encoding/json`):
`{"I":null,"Val":"456"}`

Actual result:
`{null,"Val":"456"}`

This can be reproduced with:
`github.com/goccy/go-json v0.10.0`

Pull request:
fix: fixed handling of anonymous fields other than struct
fix #426

Patch:
diff --git a/internal/encoder/compiler.go b/internal/encoder/compiler.go
index 3b3ff3f..3ae39ba 100644
--- a/internal/encoder/compiler.go
+++ b/internal/encoder/compiler.go
@@ -617,6 +617,13 @@ func (c *Compiler) structCode(typ *runtime.Type, isPtr bool) (*StructCode, error
 	return code, nil
 }
 
+func toElemType(t *runtime.Type) *runtime.Type {
+	for t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	return t
+}
+
 func (c *Compiler) structFieldCode(structCode *StructCode, tag *runtime.StructTag, isPtr, isOnlyOneFirstField bool) (*StructFieldCode, error) {
 	field := tag.Field
 	fieldType := runtime.Type2RType(field.Type)
@@ -626,7 +633,7 @@ func (c *Compiler) structFieldCode(structCode *StructCode, tag *runtime.StructTa
 		key:           tag.Key,
 		tag:           tag,
 		offset:        field.Offset,
-		isAnonymous:   field.Anonymous && !tag.IsTaggedKey,
+		isAnonymous:   field.Anonymous && !tag.IsTaggedKey && toElemType(fieldType).Kind() == reflect.Struct,
 		isTaggedKey:   tag.IsTaggedKey,
 		isNilableType: c.isNilableType(fieldType),
 		isNilCheck:    true,


Test patch:
diff --git a/encode_test.go b/encode_test.go
index 63740f3..3ff3fcb 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -2629,3 +2629,18 @@ func TestCustomMarshalForMapKey(t *testing.T) {
 	assertErr(t, err)
 	assertEq(t, "custom map key", string(expected), string(got))
 }
+
+func TestIssue426(t *testing.T) {
+	type I interface {
+		Foo()
+	}
+	type A struct {
+		I
+		Val string
+	}
+	var s A
+	s.Val = "456"
+
+	b, _ := json.Marshal(s)
+	assertEq(t, "unexpected result", `{"I":null,"Val":"456"}`, string(b))
+}