github.com/Finschia/finschia-sdk@v0.48.1/codec/codec.go (about) 1 package codec 2 3 import ( 4 "github.com/gogo/protobuf/proto" 5 6 "github.com/Finschia/finschia-sdk/codec/types" 7 ) 8 9 type ( 10 // Codec defines a functionality for serializing other objects. 11 // Users can defin a custom Protobuf-based serialization. 12 // Note, Amino can still be used without any dependency on Protobuf. 13 // SDK provides to Codec implementations: 14 // 15 // 1. AminoCodec: Provides full Amino serialization compatibility. 16 // 2. ProtoCodec: Provides full Protobuf serialization compatibility. 17 Codec interface { 18 BinaryCodec 19 JSONCodec 20 } 21 22 BinaryCodec interface { 23 // Marshal returns binary encoding of v. 24 Marshal(o ProtoMarshaler) ([]byte, error) 25 // MustMarshal calls Marshal and panics if error is returned. 26 MustMarshal(o ProtoMarshaler) []byte 27 28 // MarshalLengthPrefixed returns binary encoding of v with bytes length prefix. 29 MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error) 30 // MustMarshalLengthPrefixed calls MarshalLengthPrefixed and panics if 31 // error is returned. 32 MustMarshalLengthPrefixed(o ProtoMarshaler) []byte 33 34 // Unmarshal parses the data encoded with Marshal method and stores the result 35 // in the value pointed to by v. 36 Unmarshal(bz []byte, ptr ProtoMarshaler) error 37 // MustUnmarshal calls Unmarshal and panics if error is returned. 38 MustUnmarshal(bz []byte, ptr ProtoMarshaler) 39 40 // Unmarshal parses the data encoded with UnmarshalLengthPrefixed method and stores 41 // the result in the value pointed to by v. 42 UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error 43 // MustUnmarshalLengthPrefixed calls UnmarshalLengthPrefixed and panics if error 44 // is returned. 45 MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) 46 47 // MarshalInterface is a helper method which will wrap `i` into `Any` for correct 48 // binary interface (de)serialization. 49 MarshalInterface(i proto.Message) ([]byte, error) 50 // UnmarshalInterface is a helper method which will parse binary enoded data 51 // into `Any` and unpack any into the `ptr`. It fails if the target interface type 52 // is not registered in codec, or is not compatible with the serialized data 53 UnmarshalInterface(bz []byte, ptr interface{}) error 54 55 types.AnyUnpacker 56 } 57 58 JSONCodec interface { 59 // MarshalJSON returns JSON encoding of v. 60 MarshalJSON(o proto.Message) ([]byte, error) 61 // MustMarshalJSON calls MarshalJSON and panics if error is returned. 62 MustMarshalJSON(o proto.Message) []byte 63 // MarshalInterfaceJSON is a helper method which will wrap `i` into `Any` for correct 64 // JSON interface (de)serialization. 65 MarshalInterfaceJSON(i proto.Message) ([]byte, error) 66 // UnmarshalInterfaceJSON is a helper method which will parse JSON enoded data 67 // into `Any` and unpack any into the `ptr`. It fails if the target interface type 68 // is not registered in codec, or is not compatible with the serialized data 69 UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error 70 71 // UnmarshalJSON parses the data encoded with MarshalJSON method and stores the result 72 // in the value pointed to by v. 73 UnmarshalJSON(bz []byte, ptr proto.Message) error 74 // MustUnmarshalJSON calls Unmarshal and panics if error is returned. 75 MustUnmarshalJSON(bz []byte, ptr proto.Message) 76 } 77 78 // ProtoMarshaler defines an interface a type must implement to serialize itself 79 // as a protocol buffer defined message. 80 ProtoMarshaler interface { 81 proto.Message // for JSON serialization 82 83 Marshal() ([]byte, error) 84 MarshalTo(data []byte) (n int, err error) 85 MarshalToSizedBuffer(dAtA []byte) (int, error) 86 Size() int 87 Unmarshal(data []byte) error 88 } 89 90 // AminoMarshaler defines an interface a type must implement to serialize itself 91 // for Amino codec. 92 AminoMarshaler interface { 93 MarshalAmino() ([]byte, error) 94 UnmarshalAmino([]byte) error 95 MarshalAminoJSON() ([]byte, error) 96 UnmarshalAminoJSON([]byte) error 97 } 98 )