UnmarshalJSON导致堆栈溢出

编程入门 行业动态 更新时间:2024-10-24 13:26:36
本文介绍了UnmarshalJSON导致堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此作品: play.golang/p/-Kv3xAguDR 。

这会导致堆栈溢出: play.golang/p/1-AsHFj51O 。

我不明白为什么。在这种情况下,使用 JSONUnmarshaler 接口的正确方法是什么?

//bytesencoding / jsonfmtstrings) type T interface { Printer()} type A struct {JA string} func(t A )打印机(){fmt.Print(A)} $ b / * func(t * A)UnmarshalJSON(data [] byte)错误{i:= A {} dec:= json.NewDecoder(bytes.NewReader(data)) if err:= dec.Decode(& i); err!= nil { return err } i.Printer() * t = i return nil } * / $ b var vI [] T func main(){ vI = [] T {& A {}} get()} func get(){ dec:= json.NewDecoder(strings.NewReader([{\JA \:\OK\ }])) if err:= dec.Decode(& vI); err!= nil { fmt.Print(err)} for _,v:= range vI { v.Printer()}

解决方案

dec.Decode(& i)

会调用你的 UnmarshalJSON ,这又会调用 Decode ,依此类推。如果你需要解组你的JSON,然后对它做些什么,一个简单的技巧是声明一个本地类型,解组你的数据,然后转换回你想要的类型:

//输入a没有UnmarshalJSON。 键入a i:= a {} dec:= json.NewDecoder(bytes.NewReader(data)) if err:= dec.Decode(& i) ; err!= nil { return err } //转换回A. tt:= A(i) tt.Printer() * t = tt // ...

Playground: play.golang/p/HWamV3MbvW 。

类型 a 没有方法(所以没有堆栈溢出),但是 convertible to A 。

This works: play.golang/p/-Kv3xAguDR.

This results in a stack overflow: play.golang/p/1-AsHFj51O.

I fail to understand why. What is the correct way use the JSONUnmarshaler interface in this case?

package main import ( //"bytes" "encoding/json" "fmt" "strings" ) type T interface { Printer() } type A struct{ JA string } func (t A) Printer() { fmt.Print("A") } /* func (t *A) UnmarshalJSON(data []byte) error { i := A{} dec := json.NewDecoder(bytes.NewReader(data)) if err := dec.Decode(&i); err != nil { return err } i.Printer() *t = i return nil } */ var vI []T func main() { vI = []T{&A{}} get() } func get() { dec := json.NewDecoder(strings.NewReader("[{\"JA\":\"OK\"}]")) if err := dec.Decode(&vI); err != nil { fmt.Print(err) } for _, v := range vI { v.Printer() } }

解决方案

This

dec.Decode(&i)

will call your UnmarshalJSON, which in turn will call Decode, and so on. If you need to unmarshal your JSON and then do something with it, one neat technique is to declare a local type, unmarshal your data into it, and then convert back to your desired type:

// Type a has no UnmarshalJSON. type a A i := a{} dec := json.NewDecoder(bytes.NewReader(data)) if err := dec.Decode(&i); err != nil { return err } // Convert back to A. tt := A(i) tt.Printer() *t = tt // ...

Playground: play.golang/p/HWamV3MbvW.

The type a has no methods (so no stack overflow), but is convertible to A.

更多推荐

UnmarshalJSON导致堆栈溢出

本文发布于:2023-11-09 09:57:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   UnmarshalJSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!