001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.jackson;
017
018import java.io.IOException;
019import java.text.SimpleDateFormat;
020import java.time.Instant;
021import java.util.Date;
022import java.util.TimeZone;
023
024import com.fasterxml.jackson.core.JsonGenerator;
025import com.fasterxml.jackson.core.JsonParser;
026import com.fasterxml.jackson.core.JsonToken;
027import com.fasterxml.jackson.databind.DeserializationContext;
028import com.fasterxml.jackson.databind.JsonDeserializer;
029import com.fasterxml.jackson.databind.JsonMappingException;
030import com.fasterxml.jackson.databind.JsonSerializer;
031import com.fasterxml.jackson.databind.SerializerProvider;
032import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
033
034/**
035 * Jackson {@link JsonSerializer} classes for {@link Date}s with specified formats.
036 */
037public class DateSerde {
038
039  private static final SimpleDateFormat NO_TIMEZONE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
040
041  static {
042    NO_TIMEZONE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
043  }
044
045  /**
046   * Jackson {@link JsonSerializer} for {@link Date}.
047   */
048  public static class NoTimezoneDateJsonSerializer extends JsonSerializer<Date> {
049
050    @Override
051    public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
052      // Empty fields aren't included in the JSON.
053      if (value != null) {
054        jgen.writeString(NO_TIMEZONE_FORMAT.format(value));
055      }
056    }
057  }
058
059  /**
060   * Jackson {@link JsonDeserializer} for {@link Date}s formatted above, falling back to the Jackson way.
061   */
062  public static class FlexibleDateJsonDeserializer extends DateDeserializers.DateDeserializer {
063
064    @Override
065    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
066      if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
067        String text = jp.getText();
068        if (text.length() == 19) {
069          return Date.from(Instant.parse(text + "Z"));
070        } else {
071          return super.deserialize(jp, ctxt);
072        }
073      }
074      throw JsonMappingException.from(jp, "Expected String");
075    }
076  }
077}