본문 바로가기
자율주행소식

Protocol Buffers Java Example

by xground 2020. 2. 24.
반응형

컴파일러 및 Exam 다운로드

(현, v3.11.4)

컴파일러

Exam

.proto 준비

(Exam에서 ~\protobuf-3.11.4\examples\addressbook.proto)

// See README.txt for information and build instructions.
//
// Note: START and END tags are used in comments to define sections used in
// tutorials.  They are not part of the syntax for Protocol Buffers.
//
// To get an in-depth walkthrough of this file and the related examples, see:
// https://developers.google.com/protocol-buffers/docs/tutorials

// [START declaration]
syntax = "proto3";
package tutorial;

import "google/protobuf/timestamp.proto";
// [END declaration]

// [START java_declaration]
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
// [END java_declaration]

// [START csharp_declaration]
option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
// [END csharp_declaration]

// [START messages]
message Person {
  string name = 1;
  int32 id = 2;  // Unique ID number for this person.
  string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;

  google.protobuf.Timestamp last_updated = 5;
}

// Our address book file is just one of these.
message AddressBook {
  repeated Person people = 1;
}
// [END messages]

`message` 는 직렬화해서 전달될 대상이다.

Person은 5개의 field가 정의 되어 있다. name, id, email, phones, last_updated 

AddressBook은 1개의 field가 정의 되어 있다. people

(message명은 CamelCase, field명은 underscore_separated_names 형태로 사용한다.)

 

선언식에서 숫자값은 전달시 사이즈를 작게 해주기 위한 맵핑 값이므로 변경되서는 안되고 일정하여야 한다.

 

protoc.exe 실행

원하는 위치에서 실행

 

Java 파일 생성됨

 

반응형

댓글