package sgc.portal.base.format;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import sgc.portal.base.util.DateTime;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateTimeJsonSerializer extends JsonSerializer<DateTime> {
    private final DateFormat format;

    public DateTimeJsonSerializer() {
        this(new SimpleDateFormat("yyyyMMdd"));
    }

    public DateTimeJsonSerializer(DateFormat format) {
        this.format = format;
    }

    @Override
    public void serialize(DateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value != null) {
            gen.writeString(format.format(value));
        }
    }

    @Override
    public Class<DateTime> handledType() {
        return DateTime.class;
    }
}
