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.time.LocalDateTime; 020 021import com.fasterxml.jackson.core.JsonGenerator; 022import com.fasterxml.jackson.core.JsonParser; 023import com.fasterxml.jackson.databind.DeserializationContext; 024import com.fasterxml.jackson.databind.JsonDeserializer; 025import com.fasterxml.jackson.databind.JsonSerializer; 026import com.fasterxml.jackson.databind.SerializerProvider; 027 028/** 029 * Jackson {@link JsonSerializer} and Jackson {@link JsonDeserializer} classes for {@link 030 * LocalDateTime}. 031 */ 032public class LocalDateTimeSerDe { 033 034 /** 035 * Jackson {@link JsonSerializer} for {@link LocalDateTime}. 036 */ 037 public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { 038 039 @Override 040 public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider) 041 throws IOException { 042 jgen.writeString((value.toString())); 043 } 044 } 045 046 /** 047 * Jackson {@link JsonDeserializer} for {@link LocalDateTime}. 048 */ 049 public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { 050 051 @Override 052 public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) 053 throws IOException { 054 return LocalDateTime.parse(jp.getText()); 055 } 056 } 057}