github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/whisper/whisperv5/gen_message_json.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  
    11  package whisperv5
    12  
    13  import (
    14  	"encoding/json"
    15  
    16  	"github.com/ethereum/go-ethereum/common/hexutil"
    17  )
    18  
    19  var _ = (*messageOverride)(nil)
    20  
    21  func (m Message) MarshalJSON() ([]byte, error) {
    22  	type Message struct {
    23  		Sig       hexutil.Bytes `json:"sig,omitempty"`
    24  		TTL       uint32        `json:"ttl"`
    25  		Timestamp uint32        `json:"timestamp"`
    26  		Topic     TopicType     `json:"topic"`
    27  		Payload   hexutil.Bytes `json:"payload"`
    28  		Padding   hexutil.Bytes `json:"padding"`
    29  		PoW       float64       `json:"pow"`
    30  		Hash      hexutil.Bytes `json:"hash"`
    31  		Dst       hexutil.Bytes `json:"recipientPublicKey,omitempty"`
    32  	}
    33  	var enc Message
    34  	enc.Sig = m.Sig
    35  	enc.TTL = m.TTL
    36  	enc.Timestamp = m.Timestamp
    37  	enc.Topic = m.Topic
    38  	enc.Payload = m.Payload
    39  	enc.Padding = m.Padding
    40  	enc.PoW = m.PoW
    41  	enc.Hash = m.Hash
    42  	enc.Dst = m.Dst
    43  	return json.Marshal(&enc)
    44  }
    45  
    46  func (m *Message) UnmarshalJSON(input []byte) error {
    47  	type Message struct {
    48  		Sig       *hexutil.Bytes `json:"sig,omitempty"`
    49  		TTL       *uint32        `json:"ttl"`
    50  		Timestamp *uint32        `json:"timestamp"`
    51  		Topic     *TopicType     `json:"topic"`
    52  		Payload   *hexutil.Bytes `json:"payload"`
    53  		Padding   *hexutil.Bytes `json:"padding"`
    54  		PoW       *float64       `json:"pow"`
    55  		Hash      *hexutil.Bytes `json:"hash"`
    56  		Dst       *hexutil.Bytes `json:"recipientPublicKey,omitempty"`
    57  	}
    58  	var dec Message
    59  	if err := json.Unmarshal(input, &dec); err != nil {
    60  		return err
    61  	}
    62  	if dec.Sig != nil {
    63  		m.Sig = *dec.Sig
    64  	}
    65  	if dec.TTL != nil {
    66  		m.TTL = *dec.TTL
    67  	}
    68  	if dec.Timestamp != nil {
    69  		m.Timestamp = *dec.Timestamp
    70  	}
    71  	if dec.Topic != nil {
    72  		m.Topic = *dec.Topic
    73  	}
    74  	if dec.Payload != nil {
    75  		m.Payload = *dec.Payload
    76  	}
    77  	if dec.Padding != nil {
    78  		m.Padding = *dec.Padding
    79  	}
    80  	if dec.PoW != nil {
    81  		m.PoW = *dec.PoW
    82  	}
    83  	if dec.Hash != nil {
    84  		m.Hash = *dec.Hash
    85  	}
    86  	if dec.Dst != nil {
    87  		m.Dst = *dec.Dst
    88  	}
    89  	return nil
    90  }