All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.jackson2.InstantToSecondsFloatTimestamp Maven / Gradle / Ivy

The newest version!
package nl.vpro.jackson2;

import java.io.IOException;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;


public class InstantToSecondsFloatTimestamp {

    private InstantToSecondsFloatTimestamp() {}

    public static class Serializer extends JsonSerializer {


        public static final Serializer INSTANCE = new Serializer();

        @Override
        public void serialize(Instant value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            if (value == null) {
                jgen.writeNull();
            } else {
                jgen.writeNumber(value.toEpochMilli() / 1000f);
            }
        }
    }


    public static class Deserializer extends JsonDeserializer {

        public static final Deserializer INSTANCE = new Deserializer();
        @Override
        public Instant deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            try {
                return Instant.ofEpochMilli((long) Float.parseFloat(jp.getValueAsString()) * 1000);
            } catch (JsonParseException jpe) {
                try {
                    String s = jp.getValueAsString();
                    if (s == null) {
                        return null;
                    }
                    return Instant.parse(s);
                } catch (DateTimeParseException dtps) {
                    return ZonedDateTime.parse(jp.getValueAsString()).toInstant();
                }
            }
        }


    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy