github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/remote/serializer.go (about) 1 package remote 2 3 var ( 4 DefaultSerializerID int32 5 serializers []Serializer 6 ) 7 8 func init() { 9 RegisterSerializer(newProtoSerializer()) 10 RegisterSerializer(newJsonSerializer()) 11 } 12 13 func RegisterSerializer(serializer Serializer) { 14 serializers = append(serializers, serializer) 15 } 16 17 type Serializer interface { 18 Serialize(msg interface{}) ([]byte, error) 19 Deserialize(typeName string, bytes []byte) (interface{}, error) 20 GetTypeName(msg interface{}) (string, error) 21 } 22 23 func Serialize(message interface{}, serializerID int32) ([]byte, string, error) { 24 res, err := serializers[serializerID].Serialize(message) 25 if err != nil { 26 return nil, "", err 27 } 28 typeName, err := serializers[serializerID].GetTypeName(message) 29 if err != nil { 30 return nil, "", err 31 } 32 return res, typeName, nil 33 } 34 35 func Deserialize(message []byte, typeName string, serializerID int32) (interface{}, error) { 36 return serializers[serializerID].Deserialize(typeName, message) 37 } 38 39 // RootSerializable is the root level in-process representation of a message 40 type RootSerializable interface { 41 // Serialize returns the on-the-wire representation of the message 42 // Message -> IRootSerialized -> ByteString 43 Serialize() (RootSerialized, error) 44 } 45 46 // RootSerialized is the root level on-the-wire representation of a message 47 type RootSerialized interface { 48 // Deserialize returns the in-process representation of a message 49 // ByteString -> IRootSerialized -> Message 50 Deserialize() (RootSerializable, error) 51 }