-
-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] - Nil pointer exception #290
Comments
I cannot investigate from this information. Could you share me the reproducible code ? |
The error started to occur after latest upgrade of this library. |
Here is an example of the nil pointer error. package main
import (
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
func main() {
c := http.DefaultClient
req, _ := http.NewRequest("GET", "http://localhost:9999/bad/request", nil)
_, err := c.Do(req)
s, err := json.Marshal(struct {
Time string `json:"time"`
Data interface{} `json:"data"`
}{
Time: time.Now().Format(time.RFC3339),
Data: err,
})
if err != nil {
fmt.Println(err)
}
fmt.Println(s)
}
|
This is the reproducible test code. func TestIssue290(t *testing.T) {
type Issue290 interface {
A()
}
var a struct {
A Issue290
}
if b, err := stdjson.Marshal(a); err == nil {
t.Log(string(b))
// {"A":null}
}
if b, err := json.Marshal(a); err == nil { // panicked
t.Log(string(b))
}
} Maybe, the cause is untyped nil. |
Any expected update @goccy |
We just ran into this. Here is my reproducer which looks similar to what @orisano reported. What i find interesting is that an empty interface declaration does not trigger the problem: You need an interface with a method. package main
import (
"encoding/json"
"fmt"
goccy "github.com/goccy/go-json"
)
func main() {
type Empty interface{}
type NonEmpty interface {
A()
}
type SomeStruct struct {
Empty Empty
NonEmpty NonEmpty // Comment this line and you will receive no panic.
}
originalEncoded, originalError := json.Marshal(SomeStruct{}) // Works
fmt.Printf("Original: %s\n-> %s\n", originalEncoded, originalError)
goccyEncoded, goccyError := goccy.Marshal(SomeStruct{}) // Panics
fmt.Printf("Original: %s\n-> %s\n", goccyEncoded, goccyError)
}
@goccy hope that helps! |
Fix encoding of nil value about interface type that has method
The text was updated successfully, but these errors were encountered: