json marshal 的 Stringer 等价物

Stringer equivalent for json marshal

我可以实现 Stringer 来指定 fmt.Print() 如何将我的自定义类型写成字符串。 json marshal 是否有等价物?

我想在 go 中存储唯一值并在 json 中将其表示为一个切片。在 go 中使用切片并不是最佳选择。这个 O(n) 插入似乎是不必要的 - 二叉树或映射会更快,特别是因为我一直保持列表排序。

是的,json.Marshaler and json.Unmarshaler

实施 MarshalJSONUnmarshalJSON 会给你想要的东西。