github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/whisper/whisperv5/gen_criteria_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 12:09:51</date>
    10  //</624342688858116096>
    11  
    12  //
    13  
    14  package whisperv5
    15  
    16  import (
    17  	"encoding/json"
    18  
    19  	"github.com/ethereum/go-ethereum/common/hexutil"
    20  )
    21  
    22  var _ = (*criteriaOverride)(nil)
    23  
    24  func (c Criteria) MarshalJSON() ([]byte, error) {
    25  	type Criteria struct {
    26  		SymKeyID     string        `json:"symKeyID"`
    27  		PrivateKeyID string        `json:"privateKeyID"`
    28  		Sig          hexutil.Bytes `json:"sig"`
    29  		MinPow       float64       `json:"minPow"`
    30  		Topics       []TopicType   `json:"topics"`
    31  		AllowP2P     bool          `json:"allowP2P"`
    32  	}
    33  	var enc Criteria
    34  	enc.SymKeyID = c.SymKeyID
    35  	enc.PrivateKeyID = c.PrivateKeyID
    36  	enc.Sig = c.Sig
    37  	enc.MinPow = c.MinPow
    38  	enc.Topics = c.Topics
    39  	enc.AllowP2P = c.AllowP2P
    40  	return json.Marshal(&enc)
    41  }
    42  
    43  func (c *Criteria) UnmarshalJSON(input []byte) error {
    44  	type Criteria struct {
    45  		SymKeyID     *string        `json:"symKeyID"`
    46  		PrivateKeyID *string        `json:"privateKeyID"`
    47  		Sig          *hexutil.Bytes `json:"sig"`
    48  		MinPow       *float64       `json:"minPow"`
    49  		Topics       []TopicType    `json:"topics"`
    50  		AllowP2P     *bool          `json:"allowP2P"`
    51  	}
    52  	var dec Criteria
    53  	if err := json.Unmarshal(input, &dec); err != nil {
    54  		return err
    55  	}
    56  	if dec.SymKeyID != nil {
    57  		c.SymKeyID = *dec.SymKeyID
    58  	}
    59  	if dec.PrivateKeyID != nil {
    60  		c.PrivateKeyID = *dec.PrivateKeyID
    61  	}
    62  	if dec.Sig != nil {
    63  		c.Sig = *dec.Sig
    64  	}
    65  	if dec.MinPow != nil {
    66  		c.MinPow = *dec.MinPow
    67  	}
    68  	if dec.Topics != nil {
    69  		c.Topics = dec.Topics
    70  	}
    71  	if dec.AllowP2P != nil {
    72  		c.AllowP2P = *dec.AllowP2P
    73  	}
    74  	return nil
    75  }
    76