我想添加持久性并从json初始化。 我正在尝试保存/加载嵌套结构并得到“严重错误:堆栈溢出”。
据我了解,原因是父结构和子结构都具有指向彼此的指针,而json库正在进入循环。我需要指向父级的指针,因为需要从子级访问它。
我了解这是一个常见问题,解决该问题的最佳方法是什么?
type Mcloud struct {
Projects map[string]*Project `json:"Projects"`
Workdir string
}
type Project struct {
Name string
Networks map[string]Network
Parent *Mcloud
TFC TFConf
}
func newMcloud() *Mcloud {
mc := &Mcloud{
Projects: make(map[string]*Project),
Workdir: defaultWorkDir,
}
mc.load(statefile)
return mc
}
func (mc *Mcloud) addProject(n string) {
mc.Projects[n] = &Project{
Name: n,
Networks: make(map[string]Network),
Parent: mc,
}
mc.Projects[n].addTFConf()
}
//save saves state to statefile
func (mc *Mcloud) save(f string) (err error) {
if jsonState, err := json.Marshal(mc); err != nil {
fmt.Println("Was not able to marshal")
log.Fatal(err)
} else {
if err := ioutil.WriteFile(f, jsonState, 0666); err != nil {
fmt.Println("Was not able to write state to", f, "!")
log.Fatal(err)
}
fmt.Println("Save function saves: \n", mc, "to file ", f)
}
return err
}
func (mc *Mcloud) load(f string) (err error) {
var bytestate []byte
if bytestate, err = ioutil.ReadFile(f); err == nil {
err = json.Unmarshal(bytestate, &mc)
}
return err
}
获取
运行时:goroutine堆栈超过1000000000字节的限制致命错误: 堆栈溢出
运行时堆栈:runtime.throw(0x149cdfe,0xe) /usr/local/Cellar/go/1.11.1/libexec/src/runtime/panic.go:608 + 0x72 runtime.newstack() /usr/local/Cellar/go/1.11.1/libexec/src/runtime/stack.go:1008 + 0x729 runtime.morestack() /usr/local/Cellar/go/1.11.1/libexec/src/runtime/asm_amd64.s:429 + 0x8f
goroutine 1 [正在运行]:runtime.heapBitsSetType(0xc042a7df20、0x60, 0x60、0x1486e60) /usr/local/Cellar/go/1.11.1/libexec/src/runtime/mbitmap.go:911 + 0xa30 fp = 0xc02243c388 sp = 0xc02243c380 pc = 0x1016cd0 runtime.mallocgc(0x60,0x1486e60,0x1,0x0) /usr/local/Cellar/go/1.11.1/libexec/src/runtime/malloc.go:933 + 0x540 fp = 0xc02243c428 sp = 0xc02243c388 pc = 0x100d4f0 runtime.newobject(0x1486e60,0x0) /usr/local/Cellar/go/1.11.1/libexec/src/runtime/malloc.go:1032 + 0x38 fp = 0xc02243c458 sp = 0xc02243c428 pc = 0x100db28 Reflection.mapiterinit(0x14206a0,0xc00009d830,0x0)
答案 0 :(得分:1)
首先,您需要告诉encoding/json
跳过父字段,您可以使用json:"-"
标签。
然后在解编组期间,在加载了父级的所有子级之后,您可以遍历子级并设置其父级,然后可以通过实现json.Unmarshaler
接口将其作为解锁过程的一部分。
type Mcloud struct {
Projects map[string]*Project `json:"Projects"`
Workdir string
}
type Project struct {
Name string
Networks map[string]Network
Parent *Mcloud `json:"-"` // ignore on un/marshal
TFC TFConf
}
func (m *Mcloud) UnmarshalJSON(data []byte) error {
type tmp Mcloud
if err := json.Unmarshal(data, (*tmp)(m)); err != nil {
return err
}
// set Parent of all projects
for _, p := range m.Projects {
p.Parent = m
}
return nil
}