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

org.aaronhe.threetengson.LocalTimeConverter Maven / Gradle / Ivy

Go to download

A library provides Json serialization/deserialization support for JSR-310 backport types using Gson

The newest version!
package org.aaronhe.threetengson;

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 org.threeten.bp.LocalTime;
import org.threeten.bp.format.DateTimeFormatter;

import java.lang.reflect.Type;

public class LocalTimeConverter implements JsonSerializer, JsonDeserializer {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_TIME;

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

    @Override
    public LocalTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
          throws JsonParseException {
        return FORMATTER.parse(json.getAsString(), LocalTime.FROM);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy