일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- sqld
- thymeleaf
- db
- 싱글톤
- kafka
- 스프링
- jdbc
- JPA
- 스프링 프레임워크
- 스프링 컨테이너
- resultMap
- Javascript
- 스프링 부트 입문
- @Configuration
- Effective Java
- 생성자 주입
- DIP
- 필드 주입
- assertThrows
- java
- 스프링 빈
- DI
- spring
- 스프링 부트
- assertThat
- springboot
- 스프링부트
- mybatis
- 스프링 부트 기본
- SQL
- Today
- Total
선 조치 후 분석
GSON이란? 본문
Gson(구글 Gson, Google Gson)은 'Google Gson'의 약어로 JSON의 자바 오브젝트의 직렬화, 역직렬화를 해주는 오픈 소스 자바 라이브러리이다.
즉, 자바 객체 ↔ JSON으로 변환할 때 사용하는 라이브러리
일반적으로 사용하는 메서드는 'toJson() ' 과 'fromJson()' 이 있다. 이름에서 알 수 있듯이 'Json'으로 변환할 때는 'toJson()' Java Object로 변환할 때는 'fromJson'을 사용한다.
그리고 크게 제네릭 타입인 경우와 제네릭 타입이 아닌 경우로 나뉘는 거 같다.
1. Non-Generic Type - toJson(Object)
Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target = new MyType();
String json = gson.toJson(target); // serializes target to Json [Java Object -> Json]
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2 [Json -> Java Object]
2. Generic Type - toJson(Object src, Type typeOfSrc)
Type listType = new TypeToken<List<String>>() {}.getType();
List<String> target = new LinkedList<String>();
target.add("blah");
Gson gson = new Gson();
String json = gson.toJson(target, listType);
List<String> target2 = gson.fromJson(json, listType)
제네릭을 사용하는 경우에는 아래 내용을 참조해서 알아두자.
Object src : the object for which JSON representation is to be created (JSON 표현으로 변경되어야 할 객체)
Type typeOfSrc : The specific genericized type of src. You can obtain this type by using the TypeToken class.
(Object로 제네릭된 타입)
For example, to get the type for Collection <Foo>, you should use
Type typeOfSrc = new TypeToken <Collection <Foo>>(){}. getType();
'ETC > IT Knowledge' 카테고리의 다른 글
[STS] STS3, STS4 차이점? (0) | 2023.02.24 |
---|---|
[기본 of 기본] Repository 및 Service에서 XML 쿼리에 넘길 수 있는 파라미터 수는 1개 (0) | 2022.10.25 |
BASE64 개념 간단 정리 (1) | 2022.10.06 |
visibility vs display 차이점? (0) | 2022.10.05 |
@RestController vs @Controller 차이점 파악 (간략정리) (0) | 2022.10.05 |