일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- try-with-resources
- apache kafka
- 뉴 컨피던스
- ESG
- 히든 스토리
- 레퍼런스 복사
- CSS
- try width resources
- HTML
- sentry
- 수부타이
- docker
- Infresh
- 쿠버네티스
- 이펙티브 자바
- 부자의그릇
- node
- 공헌감
- 모두가 기다리는 사람
- Container
- kubernetes
- java
- colllection
- 월칙
- 칭기즈칸의 위대한 장군 수부타이
- 아웃풋법칙
- 과제의 분리
- 도파민형 인간
- 참조 계수
- 비메모리 자원
- Today
- Total
Hi
Protobuf(직렬화) 본문
Project(Solution)
- Module(Project): Protobuf
User 객체 -> File, Network
직렬화
java -> binary
gson -> JSON
User 객체가 직렬화를 지원하게 하기 위해서는
인터페이스 하나만 구현하면 됩니다.
객체 직렬화/역직렬화 방법
1) JSON: Gson
=> Text 형식
{ "name": "Tom", "age": 42 }
2) Binary
: 효율적으로 저장할 수 있다.
=> Google Protocol Buffers
1) 효율적이다.
2) 다양한 언어에 코드를 직접 작성할 필요가 없다.
protoc를 이용해서 생성하면 된다.
3) 타입이 명확하게 지정되어야 한다.
'타입 안정성'이 있다.
Server: Java, Python, Node.js
Client: Browser(Javascript), Android(Java/Kotlin), iOS(ObjC/Swift)
Google Protocol Buffers
: 프로토콜 작성을 위한 언어를 정의한다.
"join"
username: String
$ message.proto
$ protoc message.proto --java_out=.
$ protoc message.proto --cpp_out=.
import java.io.*;
public class Program {
public static void main(String[] args) throws Exception {
Chat.User user = Chat.User.newBuilder()
.setName("Tom")
.setAge(42)
.setAddress("Suwon")
.build();
FileOutputStream fos = new FileOutputStream("user.out");
user.writeTo(fos);
FileInputStream fis = new FileInputStream("user.out");
Chat.User copy = Chat.User.parseFrom(fis);
System.out.println(copy);
}
}
syntax = "proto2";
package protobuf;
message Word {
required string id = 1;
required string state = 2;
required string content = 3;
required int32 score = 4;
required float posX = 5;
required float posY = 6;
required float velocityY = 7;
}
message Player {
required string id = 1;
required string state = 2;
required string name = 3;
required int32 score = 4;
}
// ***2개의 메세지를 관리하는 법.
message Wrapper {
oneof msg{
Word word = 1;
Player player = 2;
}
}
'Programming > Java' 카테고리의 다른 글
종이문서로 설계 (0) | 2018.05.20 |
---|---|
gson 사용법 (0) | 2018.05.16 |
직렬화 역직렬화 (0) | 2018.05.15 |
Parameter 대입과 얕은 복사의 차이(ConcurrentModificationException) (0) | 2018.05.15 |
java Thread.sleep() 과 TimeUnit.SECONDS.sleep() (0) | 2018.05.15 |