V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
pksyqcj
V2EX  ›  Go 编程语言

请问下 golang json 化的问题,重写 UnmarshalJSON 后取不到值了.

  •  
  •   pksyqcj · 2020-08-08 23:02:00 +08:00 · 2321 次点击
    这是一个创建于 1349 天前的主题,其中的信息可能已经有所发展或是发生改变。

    关于 json 化,定义了 IdArr 的结构体,主要目的是为了接收前端字符数组,自动转换为数值型; 在嵌套到 A 中时,可以取到 ids 的值,more 的值永远为空.

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"strconv"
    )
    
    type IdArr struct {
    	Ids []uint64 `json:"ids"`
    }
    
    type A struct {
    	IdArr
    	More string `json:"more"`
    }
    
    func (s *IdArr) UnmarshalJSON(data []byte) error {
    	type Temp IdArr
    	t := struct {
    		Ids []string `json:"ids"`
    		*Temp
    	}{
    		Temp: (*Temp)(s),
    	}
    	if err := json.Unmarshal(data, &t); err != nil {
    		return err
    	}
    	for _, id := range t.Ids {
    		uId, err := strconv.ParseInt(id, 10, 64)
    		if err != nil {
    			return err
    		}
    		s.Ids = append(s.Ids, uint64(uId))
    	}
    	return nil
    }
    
    func main() {
    
    	d := `
            {"ids":["1213334"], "more": "text"}
                            `
    	a := &A{}
    	json.Unmarshal([]byte(d), &a)
    
    	fmt.Printf("%+v", a)
    }
    
    

    输出

    &{IdArr:{Ids:[1213334]} More:}
    

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

    卡了半天了,请问什么原因呢?

    8 条回复    2020-08-13 19:21:30 +08:00
    sfwn
        1
    sfwn  
       2020-08-08 23:23:55 +08:00
    unmarshalJSON 通篇和 struct A 没有任何关系,代码写错了吧
    pksyqcj
        2
    pksyqcj  
    OP
       2020-08-08 23:27:46 +08:00
    @sfwn 去除 unmarshalJSON 之后 A 就可以正常转换了,所以怀疑是重写导致了什么奇怪的 bug
    hallDrawnel
        3
    hallDrawnel  
       2020-08-08 23:59:27 +08:00
    https://stackoverflow.com/questions/53228912/unmarshal-field-of-nested-struct-not-work
    这个回答应该能够解决的问题,引用一下
    > The calling of 'json.Unmarshal' on 'Bar' will actually do the following things:
    1. calling UnmarshalJSON on Bar.
    2. Because anonymous struct in Bar don't implemented UnmarshalJSON, so calling UnmarshalJSON on it's embedding struct Foo instead.
    That's why 'Entry' in anonymous struct will not be unmarshal.
    pksyqcj
        4
    pksyqcj  
    OP
       2020-08-09 00:14:21 +08:00
    @hallDrawnel 多谢,只能换个写法了, 我直觉理解成他递归调用序列化方法了
    cby0414
        5
    cby0414  
       2020-08-09 00:42:13 +08:00
    简单的跟了一下 unmarshal,在 1.14.4 版本情况下:
    encoding/json/decode.go:620 行开始
    1. 622 行的 indirect 函数开始找实现了 unmarshaler 的结构,对应你的代码就是找到了 IdArr,然后停止
    2. 然后进入了 623 行的判断,读到本层 json 结构开始的数组下标
    3. 注意,624 行的 d.skip() 函数,将当前读取[]byte 的下标跳到了本层 json 结构结束的地方,对应楼主的代码就是跳到了这个 json 文本结束的位置
    4. 跳出后由于 d (记录解析状态的结构体)保存的读取下标已经到了 json 文本的结束,所以结束了 unmarshal

    json 库代码在实现了 unmarshaler 的情况下 unmarshal 逻辑:unmarshal 时扫描 json 文本,由于实现了 unmarshaler 接口,在库代码里,作者应该是默认 unmarshaler 会完整的自行反序列化对应的 struct,所以在库代码中这里在检测到实现了 unmarshaler 接口后,下次开始的下标就直接跳过了本层 json 结构(跳过目前开始位置的 depth 后的结构),交由 unmarshaler 实现的接口去实现。

    所以楼主的代码的问题就是:ids 和 more 是一个层级的,由于实现了 unmarshaler,unmarshal 时就将 ids 和 more 这同一层级的数据[]byte 都交由 IdArr 的 unmarshal 方法去进行,但是楼主实现的 unmarshal 只反序列化了 ids 而没有去实现 more,在跳出后,由于扫描的下标已经跳到了 json 文本的结尾,所以直接结束了 unmarshal 。

    归根到底就是库代码作者觉得你实现了某个 unmarshaler 就把这一 depth 的都交给你去自行实现然后跳过这一 depth,而作者只是实现了部分数据的 unmarshal,所以导致了问题。
    pksyqcj
        6
    pksyqcj  
    OP
       2020-08-09 00:56:24 +08:00
    @cby0414 非常详细了,多谢多谢, 我换成一层,非嵌套就 OK 了😄
    pksyqcj
        7
    pksyqcj  
    OP
       2020-08-09 00:58:00 +08:00
    @cby0414 本意是想匿名组合复用 idArr,库这么设计只能不搞嵌套了
    davidyanxw
        8
    davidyanxw  
       2020-08-13 19:21:30 +08:00
    A 继承了 IdArr 的方法,也就是 A 也实现了 UnmarshalJSON 方法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   966 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 20:43 · PVG 04:43 · LAX 13:43 · JFK 16:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.