package com.bonus.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.LocalDate;
import java.time.format.DateTimeFormatter;

public class GsonLocalDateAdapter extends TypeAdapter<LocalDate> {
   private static final DateTimeFormatter formatter;

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

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

   static {
      formatter = DateTimeFormatter.ISO_LOCAL_DATE;
   }
}
