Golang的JSON解码器无法识别类型。

huangapple go评论143阅读模式
英文:

Golang JSON decoder does not recognize type

问题

作为一个Golang初学者,我正在尝试编写一个通用函数来处理ReST请求。我通过将一个函数传递给它,该函数用于创建一个实现了接口的新资源(结构体),因为我还想在结构体上调用方法。在解码JSON时,日志记录的类型显示为正确的(结构体)类型,但是JSON解码器似乎只能识别接口,而无法解码为它。

package main

import (
	"encoding/json"
	"github.com/julienschmidt/httprouter"
	"log"
	"net/http"
	"strings"
)

// 通用资源接口
type resource interface {
	// 检查语义并返回错误数组,如果没有错误则返回nil
	check() []string
	// 在后端更新资源
	update() error
}

// 具体资源类型为"anchor"
type anchor struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

func newAnchor() resource {
	return anchor{}
}

func (a anchor) check() []string {
	return nil
}

func (a anchor) update() error {
	return nil
}

// 通用函数用于创建(POST)新资源
func restCreate(newResource func() resource) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		const F = "restCreate"
		var checkErrs []string

		res := newResource()
		log.Printf("%s res type %T\n", F, res)
		dcdr := json.NewDecoder(r.Body)
		err := dcdr.Decode(&res)
		log.Printf("%s Unmarshalled into %T: %+v\n", F, res, res)
		if err == nil {
			checkErrs = res.check()
		}
		switch {
		case err != nil:
			w.WriteHeader(http.StatusInternalServerError)
			log.Printf("[ERR] %s: %v\n", F, err)
		case checkErrs != nil:
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte(strings.Join(checkErrs, "\n")))
			log.Printf("%s: %v\n", F, err)
		default:
			res.update()
			bs, _ := json.Marshal(res)
			w.Write(bs)
		}
	}
}

func main() {
	r := httprouter.New()
	r.POST("/anchors", restCreate(newAnchor))
	http.ListenAndServe(":8080", r)
}

执行日志显示:

restCreate res type main.anchor
restCreate Unmarshalled into main.anchor: {ID: Name:}
[ERR] restCreate: json: cannot unmarshal object into Go value of type main.resource

为什么Printf显示结构体类型而json.Decoder显示接口类型?
我希望能得到任何关于出错原因以及如何以通用方式解决此问题的指示...

英文:

Still a Golang beginner, I am trying to code a generic function to serve ReST requests. I pass a function to create a new resource (struct) with an interface implemented on it, because I would also invoke methods on the struct. When decoding the JSON, logging the type shows the correct (struct) type, but the JSON decoder seems to only recognize the interface, which it cannot decode to.

package main
import (
"encoding/json"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
"strings"
)
// general resource interface
type resource interface {
// check semantics and return an array of errors or nil if no error found
check() []string
// update the resource in backend
update() error
}
// specific resource named "anchor"
type anchor struct {
ID   string `json:"id"`
Name string `json:"name"`
}
func newAnchor() resource {
return anchor{}
}
func (a anchor) check() []string {
return nil
}
func (a anchor) update() error {
return nil
}
// generic function to create (POST) a new resource
func restCreate(newResource func() resource) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
const F = "restCreate"
var checkErrs []string
res := newResource()
log.Printf("%s res type %T\n", F, res)
dcdr := json.NewDecoder(r.Body)
err := dcdr.Decode(&res)
log.Printf("%s Unmarshalled into %T: %+v\n", F, res, res)
if err == nil {
checkErrs = res.check()
}
switch {
case err != nil:
w.WriteHeader(http.StatusInternalServerError)
log.Printf("[ERR] %s: %v\n", F, err)
case checkErrs != nil:
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(strings.Join(checkErrs, "\n")))
log.Printf("%s: %v\n", F, err)
default:
res.update()
bs, _ := json.Marshal(res)
w.Write(bs)
}
}
}
func main() {
r := httprouter.New()
r.POST("/anchors", restCreate(newAnchor))
http.ListenAndServe(":8080", r)
}

The execution log shows:

restCreate res type main.anchor
restCreate Unmarshalled into main.anchor: {ID: Name:}
[ERR] restCreate: json: cannot unmarshal object into Go value of type main.resource

Why does Printf show the struct type and json.Decoder the interface?
I'd appreciate any indicator on what's going wrong and how to solve this in a generic way...

答案1

得分: 2

这是因为你试图使用一个指向接口的指针进行解组。你需要在函数中返回一个指针。

func newAnchor() resource {
    return &anchor{}
}

而且你不需要在这一行中获取地址:
err := dcdr.Decode(&res)

这里是一个简单的可工作示例:https://play.golang.org/p/3E0RmGTURO

英文:

It is because you try to use a pointer to the interface to unmarshal into. You need return a pointer in a function

func newAnchor() resource {
return &anchor{}
}

And you don't need to get address in this line:
err := dcdr.Decode(&res)

Here is small working example: https://play.golang.org/p/3E0RmGTURO

答案2

得分: 1

你不能将数据解组为接口,除非变量持有所需的具体类型的指针,因为json.Decode不知道要使用哪个具体类型。你有两种解决方法:

  1. newResource函数中返回具体类型的指针:

    func newResource() resource {
        return &anchor{}
    }
    

    这样,json.Decode就知道将JSON解组为anchor类型。

  2. restCreate函数中使用newAnchor代替newResource,这样代码更易读,也更符合惯例。

参考链接:1 http://idiomaticgo.com/post/best-practice/accept-interfaces-return-structs/

英文:

You cannot Unmarshal into an interface unless variable is holding a pointer to the concrete type you desire, as json.Decode won't know which concrete type to use. You have two workarounds at your disposal:

Having newResource return a concrete type under the hood:

func newResource() resource {
return &anchor{}
}

This way json.Decode knows to unmarshal your JSON into an anchor.

Use newAnchor instead of newResource: this will be more readable in your restCreate function, and is more idiomatic1.

1 http://idiomaticgo.com/post/best-practice/accept-interfaces-return-structs/

huangapple
  • 本文由 发表于 2017年7月17日 18:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/45142452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定