2 Star 2 Fork 6

王布衣 / gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
serialization.go 975 Bytes
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-06-07 20:08 . 修订部分util工具库
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package redblacktree
import (
"encoding/json"
"gitee.com/quant1x/gox/util/internal"
)
func assertSerializationImplementation() {
var _ internal.JSONSerializer = (*Tree)(nil)
var _ internal.JSONDeserializer = (*Tree)(nil)
}
// ToJSON outputs the JSON representation of the tree.
func (tree *Tree) ToJSON() ([]byte, error) {
elements := make(map[string]interface{})
it := tree.Iterator()
for it.Next() {
elements[internal.ToString(it.Key())] = it.Value()
}
return json.Marshal(&elements)
}
// FromJSON populates the tree from the input JSON representation.
func (tree *Tree) FromJSON(data []byte) error {
elements := make(map[string]interface{})
err := json.Unmarshal(data, &elements)
if err == nil {
tree.Clear()
for key, value := range elements {
tree.Put(key, value)
}
}
return err
}
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.21.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891