com.vaadin.flow.component.charts.model.serializers.DateSerializer Maven / Gradle / Ivy
/**
* Copyright 2000-2024 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See {@literal } for the full
* license.
*/
package com.vaadin.flow.component.charts.model.serializers;
import java.io.IOException;
import java.time.Instant;
import java.util.Date;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.vaadin.flow.component.charts.util.Util;
/**
* Serializes all {@link Date} objects as UTC long.
*/
public class DateSerializer extends JsonSerializer {
public static Module getModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(Date.class, new DateSerializer());
return module;
}
@Override
public void serialize(Date value, JsonGenerator gen,
SerializerProvider serializers)
throws IOException, JsonProcessingException {
final Instant instantUTC = value.toInstant(); // converting to UTC
gen.writeNumber(Util.toHighchartsTS(instantUTC));
}
}