Permalink
Browse files

clean up types marshalling

  • Loading branch information...
1 parent 4077c6d commit 85beb49497f78bbf6a1da3d0b2486a5f176421ad @lukechampine lukechampine committed Nov 19, 2016
Showing with 48 additions and 84 deletions.
  1. +2 −2 crypto/hash.go
  2. +1 −12 types/block.go
  3. +19 −70 types/transactions.go
  4. +26 −0 types/transactions_test.go
View
@@ -69,7 +69,7 @@ func (h *Hash) LoadString(s string) error {
}
hBytes, err := hex.DecodeString(s)
if err != nil {
- return errors.New("could not unmarshal crypto.Hash: " + err.Error())
+ return errors.New("could not unmarshal hash: " + err.Error())
}
copy(h[:], hBytes)
return nil
@@ -96,7 +96,7 @@ func (h *Hash) UnmarshalJSON(b []byte) error {
// b[1 : len(b)-1] cuts off the leading and trailing `"` in the JSON string.
hBytes, err := hex.DecodeString(string(b[1 : len(b)-1]))
if err != nil {
- return errors.New("could not unmarshal crypto.Hash: " + err.Error())
+ return errors.New("could not unmarshal hash: " + err.Error())
}
copy(h[:], hBytes)
return nil
View
@@ -5,7 +5,6 @@ package types
import (
"encoding/json"
- "errors"
"fmt"
"io"
@@ -164,15 +163,5 @@ func (bid BlockID) String() string {
// UnmarshalJSON decodes the json hex string of the block id.
func (bid *BlockID) UnmarshalJSON(b []byte) error {
- if len(b) != crypto.HashSize*2+2 {
- return crypto.ErrHashWrongLen
- }
-
- var bidBytes []byte
- _, err := fmt.Sscanf(string(b[1:len(b)-1]), "%x", &bidBytes)
- if err != nil {
- return errors.New("could not unmarshal BlockID: " + err.Error())
- }
- copy(bid[:], bidBytes)
- return nil
+ return (*crypto.Hash)(bid).UnmarshalJSON(b)
}
View
@@ -235,34 +235,33 @@ func (id SiafundOutputID) SiaClaimOutputID() SiacoinOutputID {
return SiacoinOutputID(crypto.HashObject(id))
}
-// Below this point is a bunch of repeated definitions so that all type aliases
-// of 'crypto.Hash' are printed as hex strings. A notable exception is
-// types.Target, which is still printed as a byte array.
-
-// MarshalJSON marshales a specifier as a hex string.
+// MarshalJSON marshals a specifier as a string.
func (s Specifier) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
-// String prints the specifier in hex.
+// String returns the specifier as a string, trimming any trailing zeros.
func (s Specifier) String() string {
var i int
- for i = 0; i < len(s); i++ {
+ for i = range s {
if s[i] == 0 {
break
}
}
return string(s[:i])
}
-// UnmarshalJSON decodes the json hex string of the id.
+// UnmarshalJSON decodes the json string of the specifier.
func (s *Specifier) UnmarshalJSON(b []byte) error {
- // Copy b into s, minus the json quotation marks.
- copy(s[:], b[1:len(b)-1])
+ var str string
+ if err := json.Unmarshal(b, &str); err != nil {
+ return err
+ }
+ copy(s[:], str)
return nil
}
-// MarshalJSON marshales an id as a hex string.
+// MarshalJSON marshals an id as a hex string.
func (tid TransactionID) MarshalJSON() ([]byte, error) {
return json.Marshal(tid.String())
}
@@ -274,20 +273,10 @@ func (tid TransactionID) String() string {
// UnmarshalJSON decodes the json hex string of the id.
func (tid *TransactionID) UnmarshalJSON(b []byte) error {
- if len(b) != crypto.HashSize*2+2 {
- return crypto.ErrHashWrongLen
- }
-
- var tidBytes []byte
- _, err := fmt.Sscanf(string(b[1:len(b)-1]), "%x", &tidBytes)
- if err != nil {
- return errors.New("could not unmarshal types.BlockID: " + err.Error())
- }
- copy(tid[:], tidBytes)
- return nil
+ return (*crypto.Hash)(tid).UnmarshalJSON(b)
}
-// MarshalJSON marshales an id as a hex string.
+// MarshalJSON marshals an id as a hex string.
func (oid OutputID) MarshalJSON() ([]byte, error) {
return json.Marshal(oid.String())
}
@@ -299,20 +288,10 @@ func (oid OutputID) String() string {
// UnmarshalJSON decodes the json hex string of the id.
func (oid *OutputID) UnmarshalJSON(b []byte) error {
- if len(b) != crypto.HashSize*2+2 {
- return crypto.ErrHashWrongLen
- }
-
- var oidBytes []byte
- _, err := fmt.Sscanf(string(b[1:len(b)-1]), "%x", &oidBytes)
- if err != nil {
- return errors.New("could not unmarshal types.BlockID: " + err.Error())
- }
- copy(oid[:], oidBytes)
- return nil
+ return (*crypto.Hash)(oid).UnmarshalJSON(b)
}
-// MarshalJSON marshales an id as a hex string.
+// MarshalJSON marshals an id as a hex string.
func (scoid SiacoinOutputID) MarshalJSON() ([]byte, error) {
return json.Marshal(scoid.String())
}
@@ -324,20 +303,10 @@ func (scoid SiacoinOutputID) String() string {
// UnmarshalJSON decodes the json hex string of the id.
func (scoid *SiacoinOutputID) UnmarshalJSON(b []byte) error {
- if len(b) != crypto.HashSize*2+2 {
- return crypto.ErrHashWrongLen
- }
-
- var scoidBytes []byte
- _, err := fmt.Sscanf(string(b[1:len(b)-1]), "%x", &scoidBytes)
- if err != nil {
- return errors.New("could not unmarshal types.BlockID: " + err.Error())
- }
- copy(scoid[:], scoidBytes)
- return nil
+ return (*crypto.Hash)(scoid).UnmarshalJSON(b)
}
-// MarshalJSON marshales an id as a hex string.
+// MarshalJSON marshals an id as a hex string.
func (fcid FileContractID) MarshalJSON() ([]byte, error) {
return json.Marshal(fcid.String())
}
@@ -349,20 +318,10 @@ func (fcid FileContractID) String() string {
// UnmarshalJSON decodes the json hex string of the id.
func (fcid *FileContractID) UnmarshalJSON(b []byte) error {
- if len(b) != crypto.HashSize*2+2 {
- return crypto.ErrHashWrongLen
- }
-
- var fcidBytes []byte
- _, err := fmt.Sscanf(string(b[1:len(b)-1]), "%x", &fcidBytes)
- if err != nil {
- return errors.New("could not unmarshal types.BlockID: " + err.Error())
- }
- copy(fcid[:], fcidBytes)
- return nil
+ return (*crypto.Hash)(fcid).UnmarshalJSON(b)
}
-// MarshalJSON marshales an id as a hex string.
+// MarshalJSON marshals an id as a hex string.
func (sfoid SiafundOutputID) MarshalJSON() ([]byte, error) {
return json.Marshal(sfoid.String())
}
@@ -374,15 +333,5 @@ func (sfoid SiafundOutputID) String() string {
// UnmarshalJSON decodes the json hex string of the id.
func (sfoid *SiafundOutputID) UnmarshalJSON(b []byte) error {
- if len(b) != crypto.HashSize*2+2 {
- return crypto.ErrHashWrongLen
- }
-
- var sfoidBytes []byte
- _, err := fmt.Sscanf(string(b[1:len(b)-1]), "%x", &sfoidBytes)
- if err != nil {
- return errors.New("could not unmarshal types.BlockID: " + err.Error())
- }
- copy(sfoid[:], sfoidBytes)
- return nil
+ return (*crypto.Hash)(sfoid).UnmarshalJSON(b)
}
@@ -1,6 +1,7 @@
package types
import (
+ "encoding/json"
"testing"
"github.com/NebulousLabs/Sia/crypto"
@@ -67,3 +68,28 @@ func TestTransactionSiacoinOutputSum(t *testing.T) {
t.Error("wrong siacoin output sum was calculated, got:", txn.SiacoinOutputSum())
}
}
+
+// TestSpecifierMarshaling tests the marshaling methods of the specifier
+// type.
+func TestSpecifierMarshaling(t *testing.T) {
+ s1 := SpecifierClaimOutput
+ b, err := json.Marshal(s1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var s2 Specifier
+ err = json.Unmarshal(b, &s2)
+ if err != nil {
+ t.Fatal(err)
+ } else if s2 != s1 {
+ t.Fatal("mismatch:", s1, s2)
+ }
+
+ // invalid json
+ x := 3
+ b, _ = json.Marshal(x)
+ err = json.Unmarshal(b, &s2)
+ if err == nil {
+ t.Fatal("Unmarshal should have failed")
+ }
+}

0 comments on commit 85beb49

Please sign in to comment.