Created
June 13, 2013 15:20
-
-
Save HeartSaVioR/5774559 to your computer and use it in GitHub Desktop.
기본 설정의 Gson 객체를 통해 Model -> JSON 을 변환하게 되면 Model 내 Date 필드의 밀리초 단위가 사라지게 됩니다
(결과적으로 JSON <-> Model 간의 데이터가 일치하지 않게 됩니다) 이를 방지하기 위해, Model -> JSON 변환 시 Date 필드를 밀리초 단위까지 뽑아내도록 포맷을 변경할 수 있습니다 위의 내용은 https://gist.github.com/leviwilson/2495636 를 참고하였습니다
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
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
public class GsonObjectMapper { | |
public static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; | |
private Gson gson; | |
public GsonObjectMapper() { | |
// Gson 의 기본 객체를 통해 serialize 하게 되면 밀리초 단위가 사라진다 (serialize - deserialize 가 같지 않게 됨) | |
// 이를 방지하기 위한 설정 | |
// https://gist.github.com/leviwilson/2495636 | |
gson = new GsonBuilder().setDateFormat(DATE_FORMAT).create(); | |
} | |
public <T> T deserialize(String json, java.lang.Class <T> classOfT) { | |
return gson.fromJson(json, classOfT); | |
} | |
public String serialize(Object model) { | |
return gson.toJson(model); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment