Browse Source

Support json encode/decode for nil values in S structs

pull/1782/head
Ethan Frey 8 years ago
parent
commit
8c9b889ccf
4 changed files with 27 additions and 3 deletions
  1. +24
    -0
      encode_test.go
  2. +1
    -1
      priv_key.go
  3. +1
    -1
      pub_key.go
  4. +1
    -1
      signature.go

+ 24
- 0
encode_test.go View File

@ -84,5 +84,29 @@ func TestKeyEncodings(t *testing.T) {
checkJSON(t, pubKey, &pub3, tc.keyName)
assert.EqualValues(t, pubKey, pub3)
}
}
func toFromJSON(t *testing.T, in interface{}, recvr interface{}) {
js, err := data.ToJSON(in)
require.Nil(t, err, "%+v", err)
err = data.FromJSON(js, recvr)
require.Nil(t, err, "%+v", err)
}
func TestNilEncodings(t *testing.T) {
// make sure sigs are okay with nil
a, b := SignatureS{}, SignatureS{}
toFromJSON(t, a, &b)
assert.EqualValues(t, a, b)
// make sure sigs are okay with nil
c, d := PubKeyS{}, PubKeyS{}
toFromJSON(t, c, &d)
assert.EqualValues(t, c, d)
// make sure sigs are okay with nil
e, f := PrivKeyS{}, PrivKeyS{}
toFromJSON(t, e, &f)
assert.EqualValues(t, e, f)
}

+ 1
- 1
priv_key.go View File

@ -47,7 +47,7 @@ func (p PrivKeyS) MarshalJSON() ([]byte, error) {
func (p *PrivKeyS) UnmarshalJSON(data []byte) (err error) {
parsed, err := privKeyMapper.FromJSON(data)
if err == nil {
if err == nil && parsed != nil {
p.PrivKey = parsed.(PrivKey)
}
return


+ 1
- 1
pub_key.go View File

@ -49,7 +49,7 @@ func (p PubKeyS) MarshalJSON() ([]byte, error) {
func (p *PubKeyS) UnmarshalJSON(data []byte) (err error) {
parsed, err := pubKeyMapper.FromJSON(data)
if err == nil {
if err == nil && parsed != nil {
p.PubKey = parsed.(PubKey)
}
return


+ 1
- 1
signature.go View File

@ -45,7 +45,7 @@ func (p SignatureS) MarshalJSON() ([]byte, error) {
func (p *SignatureS) UnmarshalJSON(data []byte) (err error) {
parsed, err := sigMapper.FromJSON(data)
if err == nil {
if err == nil && parsed != nil {
p.Signature = parsed.(Signature)
}
return


Loading…
Cancel
Save