Protocol buffers is a technology used to serialize structured data. It supports multiple languages, such as C ++, Java, and Python.
Persistent data or serialized data transmitted over the network. Compared with some other XML technologies, this technology is more space-saving.
Stream storage), faster and more flexible.
Create the Person. proto File
Message Person {
Optional int64 id = 1;
Optional string name = 2;
Optional string sex = 3;
Optional string tel = 4;
}
Person defines a message type as Person
Person has three fields.
Optional (optional)
Int64 corresponds to java's long
String corresponds to the java String
In the message definition, each field has a unique identifier, which can be understood as an index used to identify each field.
Id = 1, name = 2, sex = 3, tel =, 4 is not the default value
The default value can be set as follows: optional int32 result_per_page = 3 [default = 10];
Required: a message in good format must contain one such field. This parameter must be set;
Optional: In message format, this field can have 0 or 1 values (no more than 1)
Repeated: in a message in good format, this field can be repeated for any number of times (including 0 times ). The order of repeated values is retained. Indicates that the value can be repeated, which is equivalent to the List in java
For some historical reasons, the repeated fields of the basic numeric type are not encoded as efficiently as possible. In the new code, you should use the special option [packed = true] to ensure more efficient encoding.
Repeated int32 samples = 4 [packed = true];
Download: protoc-2.4.1-win32.zip for generating java files
Download client: protobuf-java-2.4.1.jar, user client call
You can describe person.protoenter the protoc.exe level-1 directory in protoc-2.4.1-win32.zip.
Then run the cmd cd command to execute protoc.exe -- java_out =./Person. proto in this directory. At this time, the Person. java file is generated in this directory.
On the java client
Serialization:
Person. Persona. Builder B = Person. Persona. newBuilder ();
B. setId (I );
B. setName ("ajun" + 1 );
B. setSex ("male ");
B. setTel ("6086666 ");
Person. Persona p = B. build ();
Byte [] value = p. toByteArray ()
Deserialization:
Person. Persona last = Person. Persona. parseFrom (person );
Proto type --- java type
. Proto type
Java type
C ++ type
Remarks
Double
Double
Double
Float
Float
Float
Int32
Int
Int32
The variable length encoding method is used. Inefficient at coding negative numbers-use sint32 if your field may contain negative numbers.
Int64
Long
Int64
The variable length encoding method is used. Inefficient at coding negative numbers-use sint64 if your field may contain negative numbers.
Uint32
Int [1]
Uint32
Uses variable-length encoding.
Uint64
Long [1]
Uint64
Uses variable-length encoding.
Sint32
Int
Int32
The variable length encoding method is used. Signed integer value. It is more efficient than the common int32 encoding.
Sint64
Long
Int64
The variable length encoding method is used. Signed integer value. Encoding is more efficient than normal int64 encoding.
Fixed32
Int [1]
Uint32
It is always 4 bytes. If the value is always greater than 228, this type is more efficient than uint32.
Fixed64
Long [1]
Uint64
It is always 8 bytes. If the value is always greater than 256, this type is more efficient than uint64.
Sfixed32
Int
Int32
It is always 4 bytes.
Sfixed64
Long
Int64
It is always 8 bytes.
Bool
Boolean
Bool
String
String
String
A string must be either UTF-8-encoded or 7-bit ASCII-encoded text.
Bytes
ByteString
String
It may contain byte data in any order.
Author: ajun_studio