github.com/cosmos/cosmos-sdk@v0.50.10/codec/amino_codec.go (about) 1 package codec 2 3 import ( 4 "github.com/cosmos/gogoproto/proto" 5 ) 6 7 // Deprecated: AminoCodec defines a codec that utilizes Codec for both binary and JSON 8 // encoding. Any usage of amino should be done using the LegacyAmino type directly. 9 // Usage of amino with the Codec type is not well-supported and may be removed in the future. 10 type AminoCodec struct { 11 *LegacyAmino 12 } 13 14 var ( 15 _ BinaryCodec = &AminoCodec{} 16 _ JSONCodec = &AminoCodec{} 17 ) 18 19 // Deprecated: NewAminoCodec returns a reference to a new AminoCodec. 20 // Use NewLegacyAmino instead. 21 func NewAminoCodec(codec *LegacyAmino) *AminoCodec { 22 return &AminoCodec{LegacyAmino: codec} 23 } 24 25 // Marshal implements BinaryMarshaler.Marshal method. 26 func (ac *AminoCodec) Marshal(o proto.Message) ([]byte, error) { 27 return ac.LegacyAmino.Marshal(o) 28 } 29 30 // MustMarshal implements BinaryMarshaler.MustMarshal method. 31 func (ac *AminoCodec) MustMarshal(o proto.Message) []byte { 32 return ac.LegacyAmino.MustMarshal(o) 33 } 34 35 // MarshalLengthPrefixed implements BinaryMarshaler.MarshalLengthPrefixed method. 36 func (ac *AminoCodec) MarshalLengthPrefixed(o proto.Message) ([]byte, error) { 37 return ac.LegacyAmino.MarshalLengthPrefixed(o) 38 } 39 40 // MustMarshalLengthPrefixed implements BinaryMarshaler.MustMarshalLengthPrefixed method. 41 func (ac *AminoCodec) MustMarshalLengthPrefixed(o proto.Message) []byte { 42 return ac.LegacyAmino.MustMarshalLengthPrefixed(o) 43 } 44 45 // Unmarshal implements BinaryMarshaler.Unmarshal method. 46 func (ac *AminoCodec) Unmarshal(bz []byte, ptr proto.Message) error { 47 return ac.LegacyAmino.Unmarshal(bz, ptr) 48 } 49 50 // MustUnmarshal implements BinaryMarshaler.MustUnmarshal method. 51 func (ac *AminoCodec) MustUnmarshal(bz []byte, ptr proto.Message) { 52 ac.LegacyAmino.MustUnmarshal(bz, ptr) 53 } 54 55 // UnmarshalLengthPrefixed implements BinaryMarshaler.UnmarshalLengthPrefixed method. 56 func (ac *AminoCodec) UnmarshalLengthPrefixed(bz []byte, ptr proto.Message) error { 57 return ac.LegacyAmino.UnmarshalLengthPrefixed(bz, ptr) 58 } 59 60 // MustUnmarshalLengthPrefixed implements BinaryMarshaler.MustUnmarshalLengthPrefixed method. 61 func (ac *AminoCodec) MustUnmarshalLengthPrefixed(bz []byte, ptr proto.Message) { 62 ac.LegacyAmino.MustUnmarshalLengthPrefixed(bz, ptr) 63 } 64 65 // MarshalJSON implements JSONCodec.MarshalJSON method, 66 // it marshals to JSON using legacy amino codec. 67 func (ac *AminoCodec) MarshalJSON(o proto.Message) ([]byte, error) { 68 return ac.LegacyAmino.MarshalJSON(o) 69 } 70 71 // MustMarshalJSON implements JSONCodec.MustMarshalJSON method, 72 // it executes MarshalJSON except it panics upon failure. 73 func (ac *AminoCodec) MustMarshalJSON(o proto.Message) []byte { 74 return ac.LegacyAmino.MustMarshalJSON(o) 75 } 76 77 // UnmarshalJSON implements JSONCodec.UnmarshalJSON method, 78 // it unmarshals from JSON using legacy amino codec. 79 func (ac *AminoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { 80 return ac.LegacyAmino.UnmarshalJSON(bz, ptr) 81 } 82 83 // MustUnmarshalJSON implements JSONCodec.MustUnmarshalJSON method, 84 // it executes UnmarshalJSON except it panics upon failure. 85 func (ac *AminoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) { 86 ac.LegacyAmino.MustUnmarshalJSON(bz, ptr) 87 } 88 89 // MarshalInterface is a convenience function for amino marshaling interfaces. 90 // The `i` must be an interface. 91 // NOTE: to marshal a concrete type, you should use Marshal instead 92 func (ac *AminoCodec) MarshalInterface(i proto.Message) ([]byte, error) { 93 if err := assertNotNil(i); err != nil { 94 return nil, err 95 } 96 return ac.LegacyAmino.Marshal(i) 97 } 98 99 // UnmarshalInterface is a convenience function for amino unmarshaling interfaces. 100 // `ptr` must be a pointer to an interface. 101 // NOTE: to unmarshal a concrete type, you should use Unmarshal instead 102 // 103 // Example: 104 // 105 // var x MyInterface 106 // err := cdc.UnmarshalInterface(bz, &x) 107 func (ac *AminoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error { 108 return ac.LegacyAmino.Unmarshal(bz, ptr) 109 } 110 111 // MarshalInterfaceJSON is a convenience function for amino marshaling interfaces. 112 // The `i` must be an interface. 113 // NOTE: to marshal a concrete type, you should use MarshalJSON instead 114 func (ac *AminoCodec) MarshalInterfaceJSON(i proto.Message) ([]byte, error) { 115 if err := assertNotNil(i); err != nil { 116 return nil, err 117 } 118 return ac.LegacyAmino.MarshalJSON(i) 119 } 120 121 // UnmarshalInterfaceJSON is a convenience function for amino unmarshaling interfaces. 122 // `ptr` must be a pointer to an interface. 123 // NOTE: to unmarshal a concrete type, you should use UnmarshalJSON instead 124 // 125 // Example: 126 // 127 // var x MyInterface 128 // err := cdc.UnmarshalInterfaceJSON(bz, &x) 129 func (ac *AminoCodec) UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error { 130 return ac.LegacyAmino.UnmarshalJSON(bz, ptr) 131 }