ran*_*chi 1 json struct embedding go
为了与大摇大摆进行交互,我需要制作一个自定义BigInt结构,该结构仅能绕过go's big.Int。
type BigInt struct {
big.Int
}
...
type SpendTx struct {
SenderID string `json:"sender_id,omitempty"`
RecipientID string `json:"recipient_id,omitempty"`
Amount utils.BigInt `json:"amount,omitempty"`
Fee utils.BigInt `json:"fee,omitempty"`
Payload string `json:"payload,omitempty"`
TTL uint64 `json:"ttl,omitempty"`
Nonce uint64 `json:"nonce,omitempty"`
}
func (t SpendTx) JSON() (output []byte, err error) {
return json.Marshal(t)
}
Run Code Online (Sandbox Code Playgroud)
我希望SpendTx.JSON()最终会打来电话big.Int.MarshalJSON(),会回来的0。相反,我得到了以下输出:
{"sender_id":"alice","recipient_id":"bob","amount":{},"fee":{},"payload":"Hello World","ttl":10,"nonce":1}
但是我真正想要的是:
{"sender_id":"alice","recipient_id":"bob","amount":10,"fee":10,"payload":"Hello World","ttl":10,"nonce":1}
而且我必须添加以下代码BigInt来做到这一点:
func (b BigInt) MarshalJSON() ([]byte, error) {
return b.Int.MarshalJSON()
}
Run Code Online (Sandbox Code Playgroud)
但是根据Effective Go的关于嵌入结构的部分,这根本没有必要。为什么big.Int显示为{}?
big.Int实现自定义JSON marshaler(json.Marshaler),请参见Int.MarshalJSON()。但是此方法具有指针接收器,因此只有在具有指针值:的情况下,才使用/调用该方法*big.Int。
并且您嵌入了一个非指针值,因此不会调用此自定义封送拆收器,并且由于big.Int它是具有未导出字段的结构,因此您将在输出中看到一个空的JSON对象:{}。
为了使其工作,您应该使用指向您的类型的指针,例如:
Amount *utils.BigInt `json:"amount,omitempty"`
Fee *utils.BigInt `json:"fee,omitempty"`
Run Code Online (Sandbox Code Playgroud)
使用它的例子:
s := SpendTx{
SenderID: "alice",
RecipientID: "bob",
Amount: &utils.BigInt{},
Fee: &utils.BigInt{},
}
data, err := s.JSON()
fmt.Println(string(data), err)
Run Code Online (Sandbox Code Playgroud)
然后以输出为例(在Go Playground上尝试):
{"sender_id":"alice","recipient_id":"bob","amount":0,"fee":0} <nil>
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用non-pointer utils.BigInt,但是utils.BigInt应该嵌入一个指针类型:
type BigInt struct {
*big.Int
}
type SpendTx struct {
Amount utils.BigInt `json:"amount,omitempty"`
Fee utils.BigInt `json:"fee,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)
然后使用它:
s := SpendTx{
SenderID: "alice",
RecipientID: "bob",
Amount: utils.BigInt{new(big.Int)},
Fee: utils.BigInt{new(big.Int)},
}
data, err := s.JSON()
fmt.Println(string(data), err)
Run Code Online (Sandbox Code Playgroud)
并再次输出(在Go Playground上尝试):
{"sender_id":"alice","recipient_id":"bob","amount":0,"fee":0} <nil>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |