com.tomtom.speedtools.xmladapters.DateTimeZoneAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Consists of a lot of handy classes and utilities for your main Java application, like
buffers, checksum calculations, locale handling, time conversion and more.
/*
* Copyright (C) 2012-2019, TomTom (http://tomtom.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tomtom.speedtools.xmladapters;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.joda.time.DateTimeZone;
import javax.annotation.Nullable;
import java.io.IOException;
/**
* Adapter class for serializing and deserializing time zones in XML/JSON.
*/
public class DateTimeZoneAdapter {
public static class JsonDateTimeZoneSerializer extends JsonSerializer {
@SuppressWarnings({"OverlyBroadThrowsClause", "SuppressionAnnotation"})
@Override
public void serialize(final DateTimeZone t, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(t.getID());
}
}
public static class JsonDateTimeZoneDeserializer extends JsonDeserializer {
@Nullable
@SuppressWarnings({"OverlyBroadThrowsClause", "SuppressionAnnotation"})
@Override
public DateTimeZone deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
final String text = jsonParser.getText();
if (text == null) {
return null;
}
return DateTimeZone.forID(text);
}
}
}