Marshal of json.RawMessage

后端 未结 2 592
盖世英雄少女心
盖世英雄少女心 2021-02-16 00:29

Please find the code here http://play.golang.org/p/zdQ14ItNBZ

I am keeping JSON data as RawMessage, but cannot decode it out. I need the containing struct to be Marshall

相关标签:
2条回答
  • 2021-02-16 00:58

    the methods on json.RawMessage all take a pointer receiver, which is why you're not able to utilize any of them; you don't have a pointer.

    This "works" in the sense that it executes, but this is likely not the strategy that you want: http://play.golang.org/p/jYvh8nHata

    basically you need this:

    type Data struct {
        Name string
        Id   int
        Json *json.RawMessage
    }
    

    and then propagate that change through the rest of your program. What... what are you actually trying to do?

    0 讨论(0)
  • 2021-02-16 01:00

    jorellis answer is correct for versions of Go before 1.8.

    Go 1.8 and newer will correctly handle marshalling of both a pointer and non-pointer json.RawMessage.

    Fixing commit: https://github.com/golang/go/commit/1625da24106b610f89ff7a67a11581df95f8e234

    0 讨论(0)
提交回复
热议问题