当前位置:网站首页>A tour of grpc:03 - proto serialization / deserialization

A tour of grpc:03 - proto serialization / deserialization

2022-07-07 17:15:00 InfoQ

In the last one , We installed and configured protoc, Write some simple message, And use protoc Automatically generated code . In this paper , We will try proto Serialization function of , And try to serialize and deserialize between different languages .

proto go  serialize

1.  newly build sample package , To facilitate testing , We write a that can generate random data random.go
null
​2.  stay sample  Write under the package  generator.go  It is convenient for us to generate subsequent test data
null
​3.  newly build  serializer , To write   Binary serialization  file.go
null
​4.  stay serializer Write under the package json.go Appoint json Serialization rules
null
​5.  stay serializer  Write a unit test under the package  file_test.go
null
​6.  modify Makefile  Add a command to run the test
null
​7. make test Run it , Can be observed  temp  Under the folder are generated  laptop.bin and  laptop.json
null
​ Can be observed ,JSON  Serialized result size   It's about   Binary serialization size  5 times .

proto Java  serialize

1.  To write sample.Generator  Class is used to generate   Test data 【 It's equivalent to integrating random.go  and  generator.go】
null
​2.  To write  serializer.Serializer  Class is used to implement serialization
null
​3.  Next, we write a unit test SerializerTest  Test class .
null
null
​ You can also see , here  JSON The size of serialization is about the size of binary serialization 5 Double span

Language serialization test

We might as well go further , Try to make golang The result of binary serialization , use Java Deserialize back
1.  We are going to Java The serialized result is deleted , then golang The binary serialization result of ⻉ To Java Project root
2.  Above SerializerTest Unit testing , Let's add another
null
​3.  Run unit tests , You can see golang The serialized result was successfully deserialized , contrast golang and Java Of JSON serialize   result , Proved proto Cross language serialization capability .
null


null
null
null

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071459172203.html