github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/whisper/whisperv5/gen_message_json.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:46</date>
    10  //</624450124562567168>
    11  
    12  //代码由github.com/fjl/gencodec生成。不要编辑。
    13  
    14  package whisperv5
    15  
    16  import (
    17  	"encoding/json"
    18  
    19  	"github.com/ethereum/go-ethereum/common/hexutil"
    20  )
    21  
    22  var _ = (*messageOverride)(nil)
    23  
    24  func (m Message) MarshalJSON() ([]byte, error) {
    25  	type Message struct {
    26  		Sig       hexutil.Bytes `json:"sig,omitempty"`
    27  		TTL       uint32        `json:"ttl"`
    28  		Timestamp uint32        `json:"timestamp"`
    29  		Topic     TopicType     `json:"topic"`
    30  		Payload   hexutil.Bytes `json:"payload"`
    31  		Padding   hexutil.Bytes `json:"padding"`
    32  		PoW       float64       `json:"pow"`
    33  		Hash      hexutil.Bytes `json:"hash"`
    34  		Dst       hexutil.Bytes `json:"recipientPublicKey,omitempty"`
    35  	}
    36  	var enc Message
    37  	enc.Sig = m.Sig
    38  	enc.TTL = m.TTL
    39  	enc.Timestamp = m.Timestamp
    40  	enc.Topic = m.Topic
    41  	enc.Payload = m.Payload
    42  	enc.Padding = m.Padding
    43  	enc.PoW = m.PoW
    44  	enc.Hash = m.Hash
    45  	enc.Dst = m.Dst
    46  	return json.Marshal(&enc)
    47  }
    48  
    49  func (m *Message) UnmarshalJSON(input []byte) error {
    50  	type Message struct {
    51  		Sig       *hexutil.Bytes `json:"sig,omitempty"`
    52  		TTL       *uint32        `json:"ttl"`
    53  		Timestamp *uint32        `json:"timestamp"`
    54  		Topic     *TopicType     `json:"topic"`
    55  		Payload   *hexutil.Bytes `json:"payload"`
    56  		Padding   *hexutil.Bytes `json:"padding"`
    57  		PoW       *float64       `json:"pow"`
    58  		Hash      *hexutil.Bytes `json:"hash"`
    59  		Dst       *hexutil.Bytes `json:"recipientPublicKey,omitempty"`
    60  	}
    61  	var dec Message
    62  	if err := json.Unmarshal(input, &dec); err != nil {
    63  		return err
    64  	}
    65  	if dec.Sig != nil {
    66  		m.Sig = *dec.Sig
    67  	}
    68  	if dec.TTL != nil {
    69  		m.TTL = *dec.TTL
    70  	}
    71  	if dec.Timestamp != nil {
    72  		m.Timestamp = *dec.Timestamp
    73  	}
    74  	if dec.Topic != nil {
    75  		m.Topic = *dec.Topic
    76  	}
    77  	if dec.Payload != nil {
    78  		m.Payload = *dec.Payload
    79  	}
    80  	if dec.Padding != nil {
    81  		m.Padding = *dec.Padding
    82  	}
    83  	if dec.PoW != nil {
    84  		m.PoW = *dec.PoW
    85  	}
    86  	if dec.Hash != nil {
    87  		m.Hash = *dec.Hash
    88  	}
    89  	if dec.Dst != nil {
    90  		m.Dst = *dec.Dst
    91  	}
    92  	return nil
    93  }
    94