GSON @SerializedName이란? :: 행복한 프로그래머

posted by 쁘로그램어 2018. 5. 20. 22:43

Gson이란?

Gson은 자바 객체와 JSON 간의 직렬화 및 역직렬화를 위한 오픈소스 자바 라이브러리이다.

간단한 예제를 통해 알아보면 다음과 같다.


public class Person {  

    @SerializedName(value = "name")

    private String personName;


    @SerializedName(value = "bd")

    private String birthDate;

}


Person Class에는 personName, birthDate라는 2개의 field가 있다.

@SerializedName annotation의 value는 객체를 직렬화 및 역직렬화 할 때 이름으로 사용된다.

위에 personName field는 JSON에서 name으로 표출된다.

{

    "name":"cherrypick",

    "bd":"1989-10-24"

}


※ 참고 사이트 ※

http://www.feelteller.com/8

https://cherrypick.co.kr/gson-serializedname-annotation/

https://stackoverflow.com/questions/28957285/what-is-the-basic-purpose-of-serializedname-annotation-in-android-using-gson

http://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html