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

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

There is a newer version: 5.3.2
Show newest version
package nl.vpro.jackson2;

import java.io.IOException;
import java.io.Serial;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;


public class DateToJsonTimestamp {

    private DateToJsonTimestamp() {}

    public static class Serializer extends JsonSerializer {
        public static final Serializer INSTANCE = new Serializer();

        @Override
        public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            if (value == null) {
                jgen.writeNull();
            } else {
                jgen.writeNumber(value.getTime());
            }
        }
    }


    public static class Deserializer extends StdDeserializer {

        public static final Deserializer INSTANCE = new Deserializer(Date.class);
        @Serial
        private static final long serialVersionUID = -7053202522314720853L;

        protected Deserializer(Class vc) {
            super(vc);
        }

        @Override
        public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            return _parseDate(jp, ctxt);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy