MessagePackIt is an efficient binary serialization format. It can perform fast data exchange in multiple languages, such as JSON format. It is smaller and more efficient than Json, and can be used in some structured data storage, which is very suitable for use in occasions such as message bus and MemoryCache where serialization requirements are relatively high.
MessagePack is basically supported by most mainstream languages, so using it as a message serialization between tutorials is also a good way. Here is a brief introduction to how to use MessagePack in .Net.
First install the Nuget package of MessagePack and MessagePackAnalyzer:
Install-Package MessagePack Install-Package MessagePackAnalyzer
MessagePackAnalyzer is optional, but it can help check the correctness of serialized tags, which is still very useful.
The following is a simple example, which is very simple, so I won't introduce it much.
// mark MessagePackObjectAttribute [MessagePackObject] public class MyClass { // Key is serialization index, it is important for versioning. [Key(0)] public int Age { get; set; } [Key(1)] public string FirstName { get; set; } [Key(2)] public string LastName { get; set; } // public members and does not serialize target, mark IgnoreMemberttribute [IgnoreMember] public string FullName { get { return FirstName + LastName; } } } class Program { static void Main(string[] args) { var mc = new MyClass { Age = 99, FirstName = "hoge", LastName = "huga", }; // call Serialize/Deserialize, that's all. var bytes = (mc); var mc2 = <MyClass>(bytes); // you can dump msgpack binary to human readable json. // In default, MeesagePack for C# reduce property name information. // [99,"hoge","huga"] var json = (bytes); (json); } }
This is all about this article about C# using object serialization class library MessasgePack. I hope it will be helpful to everyone's learning and I hope everyone will support me more.