-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Protobuf Java | ||
|
||
>Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. | ||
## Results | ||
- `ProtobufJavaTest` | ||
- `AddressBookTest` | ||
|
||
## References | ||
- [Protocol Buffers](https://developers.google.cn/protocol-buffers?hl=zh-cn) | ||
- [Protocol Buffer Basics: Java](https://developers.google.cn/protocol-buffers/docs/javatutorial?hl=zh-cn) | ||
- [How to use Google Protocol Buffers in Java to serialize structured Data](https://roytuts.com/how-to-use-google-protocol-buffers-in-java-to-serialize-structured-data/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Protobuf Language Guide | ||
|
||
## Scalar Value Types | ||
|
||
.proto Type | C++ Type | Java/Kotlin Type | Python Type | Go Type | Ruby Type | C# Type | PHP Type | Dart Type | ||
---|---|---|---|---|---|---|---|--- | ||
double | double | double | float | float64 | Float | double | float | double | ||
float | float | float | float | float32 | Float | float | float | double | ||
int32 | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer | int | ||
int64 | int64 | long | int/long | int64 | Bignum | long | integer/string | Int64 | ||
uint32 | uint32 | int | int/long | uint32 | Fixnum or Bignum (as required) | uint | integer | int | ||
uint64 | uint64 | long | int/long | uint64 | Bignum | ulong | integer/string | Int64 | ||
sint32 | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer | int | ||
sint64 | int64 | long | int/long | int64 | Bignum | long | integer/string | Int64 | ||
fixed32 | uint32 | int | int/long | uint32 | Fixnum or Bignum (as required) | uint | integer | int | ||
fixed64 | uint64 | long | int/long | uint64 | Bignum | ulong | integer/string | Int64 | ||
sfixed32 | int64 | long | int/long | int64 | Bignum | long | integer/string | Int64 | ||
bool | bool | boolean | bool | bool | TrueClass/FalseClass | bool | boolean | bool | ||
string | string | String | str/unicode | string | String (UTF-8) | string | string | String | ||
bytes | string | ByteString | str | []byte | String (ASCII-8BIT) | ByteString | string | List | ||
|
||
## Default Values | ||
- For strings, the default value is the empty string. | ||
- For bytes, the default value is empty bytes. | ||
- For bools, the default value is false. | ||
- For numeric types, the default value is zero. | ||
- For [enums](https://developers.google.cn/protocol-buffers/docs/proto3?hl=zh-cn#enum), the default value is the **first defined enum value**, which must be 0. | ||
- For message fields, the field is not set. Its exact value is language-dependent. See the [generated code guide](https://developers.google.cn/protocol-buffers/docs/reference/overview?hl=zh-cn) for details. | ||
|
||
## References | ||
- [Language Guide (proto3)](https://developers.google.cn/protocol-buffers/docs/proto3?hl=zh-cn) |
18 changes: 18 additions & 0 deletions
18
utils/src/main/java/t5750/utils/protobuf/AddressBookTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package t5750.utils.protobuf; | ||
|
||
import t5750.utils.util.Global; | ||
|
||
public class AddressBookTest { | ||
public static void main(String[] args) { | ||
Person person = Person.newBuilder().setId(1234) | ||
.setName(Global.T5750.toUpperCase()) | ||
.setEmail(Global.T5750 + "@example.com") | ||
.addPhones(Person.PhoneNumber.newBuilder().setNumber("555-4321") | ||
.setType(Person.PhoneType.HOME)) | ||
.build(); | ||
System.out.println(person.getId()); | ||
System.out.println(person.getName()); | ||
System.out.println(person.getEmail()); | ||
System.out.println(person.getPhonesList().get(0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
syntax = "proto2"; | ||
|
||
package tutorial; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "t5750.utils.protobuf"; | ||
option java_outer_classname = "AddressBookProtos"; | ||
|
||
message Person { | ||
optional string name = 1; | ||
optional int32 id = 2; | ||
optional string email = 3; | ||
|
||
enum PhoneType { | ||
MOBILE = 0; | ||
HOME = 1; | ||
WORK = 2; | ||
} | ||
|
||
message PhoneNumber { | ||
optional string number = 1; | ||
optional PhoneType type = 2 [default = HOME]; | ||
} | ||
|
||
repeated PhoneNumber phones = 4; | ||
} | ||
|
||
message AddressBook { | ||
repeated Person people = 1; | ||
} |