package net.heartshield.data;

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.util.TimeZone;

/**
 * Google gson adapter for JSON serialization/deserialization of TimeZone objects.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2016-04-06
 */
public class TimeZoneAdapter implements JsonSerializer<TimeZone>, JsonDeserializer<TimeZone> {
    @Override
    public JsonElement serialize(TimeZone src, Type typeOfSrc, JsonSerializationContext context) {
        return new JsonPrimitive(src.getID());
    }

    @Override
    public TimeZone deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        return TimeZone.getTimeZone(json.getAsString());
    }
}
