카테고리 보관물: 자바

자바

GSON 출력시 날짜 형식 같은건 없는건가요?

Gson 출력에서 ​​사용자 정의 날짜 형식을 찾으려고하지만 .setDateFormat(DateFormat.FULL)작동하지 않는 것 같습니다 .registerTypeAdapter(Date.class, new DateSerializer()).

Gson이 “Date”개체를 신경 쓰지 않고 그 방식으로 인쇄하는 것과 같습니다.

어떻게 바꿀 수 있습니까?

미리 감사 드립니다.

@Entity
public class AdviceSheet {
  public Date lastModif;
[...]
}

public void method {
   Gson gson = new GsonBuilder().setDateFormat(DateFormat.LONG).create();
   System.out.println(gson.toJson(adviceSheet);
}

나는 항상 사용 java.util.Date한다; setDateFormat()작동하지 않습니다 🙁



답변

날짜 및 시간 부분의 형식을 정의하거나 문자열 기반 형식을 사용해야 할 것 같습니다. 예를 들면 다음과 같습니다.

Gson gson = new GsonBuilder()
   .setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();

또는 java.text.DateFormat 사용

Gson gson = new GsonBuilder()
   .setDateFormat(DateFormat.FULL, DateFormat.FULL).create();

또는 serializer로 수행하십시오.

포맷터에서 타임 스탬프를 생성 할 수는 없지만이 시리얼 라이저 / 디시리얼라이저 쌍이 작동하는 것 같습니다

JsonSerializer<Date> ser = new JsonSerializer<Date>() {
  @Override
  public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext
             context) {
    return src == null ? null : new JsonPrimitive(src.getTime());
  }
};

JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
  @Override
  public Date deserialize(JsonElement json, Type typeOfT,
       JsonDeserializationContext context) throws JsonParseException {
    return json == null ? null : new Date(json.getAsLong());
  }
};

Gson gson = new GsonBuilder()
   .registerTypeAdapter(Date.class, ser)
   .registerTypeAdapter(Date.class, deser).create();

Java 8 이상을 사용하는 경우 위의 serializer / deserializer를 다음과 같이 사용해야합니다.

JsonSerializer<Date> ser = (src, typeOfSrc, context) -> src == null ? null
            : new JsonPrimitive(src.getTime());

JsonDeserializer<Date> deser = (jSon, typeOfT, context) -> jSon == null ? null : new Date(jSon.getAsLong());

답변

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();

위의 형식은 최대 밀리미터의 정밀도를 갖기 때문에 나에게 더 좋습니다.


답변

ML이 지적했듯이 JsonSerializer는 여기서 작동합니다. 그러나 데이터베이스 엔티티를 형식화하는 경우 java.sql.Date를 사용하여 직렬 변환기를 등록하십시오. 디시리얼라이저는 필요하지 않습니다.

Gson gson = new GsonBuilder()
   .registerTypeAdapter(java.sql.Date.class, ser).create();

이 버그 보고서는 http://code.google.com/p/google-gson/issues/detail?id=230 과 관련이있을 수 있습니다 . 그래도 버전 1.7.2를 사용합니다.


답변

내부 클래스를 싫어하는 경우 기능적 인터페이스를 이용 하면 람다 식으로 Java 8 에서 더 적은 코드를 작성할 수 있습니다 .

JsonDeserializer<Date> dateJsonDeserializer =
     (json, typeOfT, context) -> json == null ? null : new Date(json.getAsLong());
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,dateJsonDeserializer).create();

답변

이것은 실제로 작동하지 않습니다. JSON에는 날짜 유형이 없습니다. 형식에 구애받지 않고 JS compat을 위해 ISO8601로 직렬화하는 것이 좋습니다. 날짜를 포함하는 필드를 알아야합니다.


답변

다른 형식 Gson gson = builder.setDateFormat("yyyy-MM-dd").create();yyyy-MM-dd사용 하는 대신이 방법 으로 형식 을 지정할 수 있습니다.

 GsonBuilder builder = new GsonBuilder();
                        builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                                return new Date(json.getAsJsonPrimitive().getAsLong());
                            }
                        });

                        Gson gson = builder.setDateFormat("yyyy-MM-dd").create();

답변

이것은 버그 입니다. 현재 timeStyle는 잘 설정 하거나 다른 답변에 설명 된 대안 중 하나를 사용해야합니다.