json - 不调用 Go json Unmarshaller

标签 json go marshalling unmarshalling

我对 Golang 还很陌生。我一直在寻找一种方法来为编码和解码 json 做一些自定义的事情。我找到了实现 MarshallerUnmarshaller 接口(interface)的解决方案。

这是我的 struct 实现接口(interface)(我也实现了 Stringer):

type Data struct {
    Foo string `json:"foo"`
    bar string
}

func (d Data) MarshalJSON() ([]byte, error) {
    return []byte("{\"foo\":\"test\",\"bar\":\"data\"}"), nil
}

func (d Data) String() string {
    return fmt.Sprintf("Foo: %s, bar: %s", d.Foo, d.bar)
}

func (d Data) UnmarshalJSON(b []byte) error {
    d.bar = "testtest"
    d.Foo = "data"
    return nil
}

对于Marshaller,一切都按预期工作:

data := &Data{}
marshal, _ := json.Marshal(data)
fmt.Println(string(marshal))

预期的输出:

{"foo":"test","bar":"data"}

但 Unmarshaller 并没有像我预期的那样工作:

jsonData := "{\"foo\":\"test\"}"
data := Data{}
json.Unmarshal([]byte(jsonData), data)
fmt.Println(data)

此代码打印:

Foo: , bar:

我在这里遗漏了什么吗?

最佳答案

这里有几个明显的问题。

  • 如果要修改接收器,方法接收器必须是指针,否则您只是在修改方法本地的副本。

  • 您始终需要解码为指针。

  • 您正在为 Foo 字段声明一个 "field" json 标记,但传入 "foo"

  • 您在 UnmarshalJSON 方法中调用 json.Unmarshal,它将无限递归。

一个工作示例看起来像

func (d *Data) UnmarshalJSON(b []byte) error {
    type data Data
    tmp := &data{bar: "bar"}
    err := json.Unmarshal(b, tmp)
    if err != nil {
        return err
    }
    *d = Data(*tmp)
    return nil
}

func main() {
    jsonData := "{\"field\":\"test\"}"
    data := Data{}
    json.Unmarshal([]byte(jsonData), &data)
    fmt.Printf("%#v\n", data)
}

https://play.golang.org/p/FnF2li63dt

关于json - 不调用 Go json Unmarshaller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46448102/

相关文章:

javascript - 反序列化 JSON 对象时,有没有办法将属性名称转换为小写?

java - 如何使用 Spring MVC 从 API REST 获取对象

Python 错误 : TypeError: Object of type 'Timestamp' is not JSON serializable'

go - 未找到类型 InteractionCallback,atom 正在删除导入语句

node.js - 当依赖包所有者从 github 中删除存储库时,Golang 项目会发生什么?

c# - 将结构编码到非托管数组

c# - 如果 GetFields() 不能保证顺序,LayoutKind.Sequential 如何工作

json - 在 postman 中发布 objectId

go - 有没有一种方法可以在多个处理程序中使用相同的request.Body,而无需手动编写大量代码,或者我需要更改执行此操作的方式?

delphi - 简单编码解码对象