package com.bonus.canteen.core.common.converter;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class GsonLocalDateTimeAdapter extends TypeAdapter<LocalDateTime> {
   private static final DateTimeFormatter formatter;

   public void write(JsonWriter out, LocalDateTime value) throws IOException {
      out.value(value.format(formatter));
   }

   public LocalDateTime read(JsonReader in) throws IOException {
      return LocalDateTime.parse(in.nextString(), formatter);
   }

   static {
      formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
   }
}
