github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/whisper/whisperv5/gen_criteria_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 _ = (*criteriaOverride)(nil)
    20  
    21  func (c Criteria) MarshalJSON() ([]byte, error) {
    22  	type Criteria struct {
    23  		SymKeyID     string        `json:"symKeyID"`
    24  		PrivateKeyID string        `json:"privateKeyID"`
    25  		Sig          hexutil.Bytes `json:"sig"`
    26  		MinPow       float64       `json:"minPow"`
    27  		Topics       []TopicType   `json:"topics"`
    28  		AllowP2P     bool          `json:"allowP2P"`
    29  	}
    30  	var enc Criteria
    31  	enc.SymKeyID = c.SymKeyID
    32  	enc.PrivateKeyID = c.PrivateKeyID
    33  	enc.Sig = c.Sig
    34  	enc.MinPow = c.MinPow
    35  	enc.Topics = c.Topics
    36  	enc.AllowP2P = c.AllowP2P
    37  	return json.Marshal(&enc)
    38  }
    39  
    40  func (c *Criteria) UnmarshalJSON(input []byte) error {
    41  	type Criteria struct {
    42  		SymKeyID     *string        `json:"symKeyID"`
    43  		PrivateKeyID *string        `json:"privateKeyID"`
    44  		Sig          *hexutil.Bytes `json:"sig"`
    45  		MinPow       *float64       `json:"minPow"`
    46  		Topics       []TopicType    `json:"topics"`
    47  		AllowP2P     *bool          `json:"allowP2P"`
    48  	}
    49  	var dec Criteria
    50  	if err := json.Unmarshal(input, &dec); err != nil {
    51  		return err
    52  	}
    53  	if dec.SymKeyID != nil {
    54  		c.SymKeyID = *dec.SymKeyID
    55  	}
    56  	if dec.PrivateKeyID != nil {
    57  		c.PrivateKeyID = *dec.PrivateKeyID
    58  	}
    59  	if dec.Sig != nil {
    60  		c.Sig = *dec.Sig
    61  	}
    62  	if dec.MinPow != nil {
    63  		c.MinPow = *dec.MinPow
    64  	}
    65  	if dec.Topics != nil {
    66  		c.Topics = dec.Topics
    67  	}
    68  	if dec.AllowP2P != nil {
    69  		c.AllowP2P = *dec.AllowP2P
    70  	}
    71  	return nil
    72  }