github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/whisper/whisperv6/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  //</624450125472731136>
    11  
    12  //代码由github.com/fjl/gencodec生成。不要编辑。
    13  
    14  package whisperv6
    15  
    16  import (
    17  	"encoding/json"
    18  
    19  	"github.com/ethereum/go-ethereum/common/hexutil"
    20  )
    21  
    22  var _ = (*messageOverride)(nil)
    23  
    24  //marshaljson将类型消息封送到json字符串
    25  func (m Message) MarshalJSON() ([]byte, error) {
    26  	type Message struct {
    27  		Sig       hexutil.Bytes `json:"sig,omitempty"`
    28  		TTL       uint32        `json:"ttl"`
    29  		Timestamp uint32        `json:"timestamp"`
    30  		Topic     TopicType     `json:"topic"`
    31  		Payload   hexutil.Bytes `json:"payload"`
    32  		Padding   hexutil.Bytes `json:"padding"`
    33  		PoW       float64       `json:"pow"`
    34  		Hash      hexutil.Bytes `json:"hash"`
    35  		Dst       hexutil.Bytes `json:"recipientPublicKey,omitempty"`
    36  	}
    37  	var enc Message
    38  	enc.Sig = m.Sig
    39  	enc.TTL = m.TTL
    40  	enc.Timestamp = m.Timestamp
    41  	enc.Topic = m.Topic
    42  	enc.Payload = m.Payload
    43  	enc.Padding = m.Padding
    44  	enc.PoW = m.PoW
    45  	enc.Hash = m.Hash
    46  	enc.Dst = m.Dst
    47  	return json.Marshal(&enc)
    48  }
    49  
    50  //将类型消息解封为JSON字符串
    51  func (m *Message) UnmarshalJSON(input []byte) error {
    52  	type Message struct {
    53  		Sig       *hexutil.Bytes `json:"sig,omitempty"`
    54  		TTL       *uint32        `json:"ttl"`
    55  		Timestamp *uint32        `json:"timestamp"`
    56  		Topic     *TopicType     `json:"topic"`
    57  		Payload   *hexutil.Bytes `json:"payload"`
    58  		Padding   *hexutil.Bytes `json:"padding"`
    59  		PoW       *float64       `json:"pow"`
    60  		Hash      *hexutil.Bytes `json:"hash"`
    61  		Dst       *hexutil.Bytes `json:"recipientPublicKey,omitempty"`
    62  	}
    63  	var dec Message
    64  	if err := json.Unmarshal(input, &dec); err != nil {
    65  		return err
    66  	}
    67  	if dec.Sig != nil {
    68  		m.Sig = *dec.Sig
    69  	}
    70  	if dec.TTL != nil {
    71  		m.TTL = *dec.TTL
    72  	}
    73  	if dec.Timestamp != nil {
    74  		m.Timestamp = *dec.Timestamp
    75  	}
    76  	if dec.Topic != nil {
    77  		m.Topic = *dec.Topic
    78  	}
    79  	if dec.Payload != nil {
    80  		m.Payload = *dec.Payload
    81  	}
    82  	if dec.Padding != nil {
    83  		m.Padding = *dec.Padding
    84  	}
    85  	if dec.PoW != nil {
    86  		m.PoW = *dec.PoW
    87  	}
    88  	if dec.Hash != nil {
    89  		m.Hash = *dec.Hash
    90  	}
    91  	if dec.Dst != nil {
    92  		m.Dst = *dec.Dst
    93  	}
    94  	return nil
    95  }
    96