Skip to content

Commit

Permalink
add MustMarshalJSON & MustUnmarshalJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Sep 20, 2018
1 parent 0c74291 commit dd9b5f3
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ func (cdc *Codec) MarshalJSON(o interface{}) ([]byte, error) {
return w.Bytes(), nil
}

// MustMarshalJSON panics if an error occurs. Besides tha behaves exactly like MarshalJSON.
func (cdc *Codec) MustMarshalJSON(o interface{}) []byte {
bz, err := cdc.MarshalJSON(o)
if err != nil {
panic(err)
}
return bz
}

func (cdc *Codec) UnmarshalJSON(bz []byte, ptr interface{}) error {
if len(bz) == 0 {
return errors.New("UnmarshalJSON cannot decode empty bytes")
Expand Down Expand Up @@ -430,6 +439,13 @@ func (cdc *Codec) UnmarshalJSON(bz []byte, ptr interface{}) error {
return cdc.decodeReflectJSON(bz, info, rv, FieldOptions{})
}

// MustUnmarshalJSON panics if an error occurs. Besides tha behaves exactly like UnmarshalJSON.
func (cdc *Codec) MustUnmarshalJSON(bz []byte, ptr interface{}) {
if err := cdc.UnmarshalJSON(bz, ptr); err != nil {
panic(err)
}
}

// MarshalJSONIndent calls json.Indent on the output of cdc.MarshalJSON
// using the given prefix and indent string.
func (cdc *Codec) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error) {
Expand Down

0 comments on commit dd9b5f3

Please sign in to comment.