goccy / go-json

Fast JSON encoder/decoder compatible with encoding/json for Go
MIT License
3.11k stars 148 forks source link

Library fails to unmarshal: cannot unmarshal main.SomeInterface into Go value of type main.SomeInterface #525

Open orangeswim opened 1 month ago

orangeswim commented 1 month ago

See code below for failing use case for go-json where it works in standard library. I'm still trying to troubleshoot the root cause. This issue came up when creating an interface for a go repository and one of their test cases were failing. I've kept only the relevant code to recreate the issue. The test also fails even if we have data no the string "{}"

Test with go v1.23, github.com/goccy/go-json v0.10.3

package main

import (
    stdjson "encoding/json"
    "fmt"

    "github.com/goccy/go-json"
)

type SomeStruct struct {
    Field string `json:"field"`
}

func (c *SomeStruct) Func1() {}

type SomeInterface interface {
    Func1()
}

func main() {

    var claims2 SomeInterface = &SomeStruct{}

    err := stdjson.Unmarshal([]byte("{\"field\":\"value\"}"), &claims2)
    if err == nil {
        fmt.Println("Standard Library ok")
    } else {
        fmt.Println("Standard Library not ok")
    }

    err = json.Unmarshal([]byte("{\"field\":\"value\"}"), &claims2)
    if err == nil {
        fmt.Println("Goccy ok")
    } else {
        fmt.Printf("Goccy not ok, %v\n", err)
    }
}
Standard Library ok
Goccy not ok, json: cannot unmarshal main.SomeInterface into Go value of type main.SomeInterface
SuperSandro2000 commented 3 weeks ago

I tried testing and benchmarking https://github.com/golang-jwt/jwt with goccy and hit the same error.

SuperSandro2000 commented 3 weeks ago

Also probably a dupe of #443