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

net.jacobpeterson.util.gson.ZonedDateTimeAdapter Maven / Gradle / Ivy

package net.jacobpeterson.util.gson;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * The GSON ZonedDateTime (de)serialization adapter.
 */
public class ZonedDateTimeAdapter implements JsonSerializer, JsonDeserializer {

    @Override
    public JsonElement serialize(ZonedDateTime src, Type typeOfSrc, JsonSerializationContext context) {
        return new JsonPrimitive(src.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
    }

    @Override
    public ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        return ZonedDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy