com.github.messenger4j.internal.gson.OptionalInstantToSecondsStringSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of messenger4j Show documentation
Show all versions of messenger4j Show documentation
A Java library for building Chatbots on the Facebook Messenger Platform - easy and fast
The newest version!
package com.github.messenger4j.internal.gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.time.Instant;
import java.util.Optional;
/**
* @author Max Grabenhorst
* @since 1.0.0
*/
public final class OptionalInstantToSecondsStringSerializer
implements JsonSerializer> {
@Override
public JsonElement serialize(
Optional src, Type typeOfSrc, JsonSerializationContext context) {
return src.map(instant -> new JsonPrimitive(Long.toString(instant.getEpochSecond())))
.orElse(null);
}
}