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

tech.simter.genson.ext.java8time.LocalTimeConverter Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package tech.simter.genson.ext.java8time;

import com.owlike.genson.Context;
import com.owlike.genson.Converter;
import com.owlike.genson.stream.ObjectReader;
import com.owlike.genson.stream.ObjectWriter;
import tech.simter.annotation.Format;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * The {@link LocalTime} {@link Converter} of Genson.
 * 

* Can use {@link Format @Format("HH:mm:ss")} on filed or getter to config the formatter. * The default format is {@link DateTimeFormatter#ISO_LOCAL_TIME}. * * @author RJ */ public class LocalTimeConverter implements Converter { private final static DateTimeFormatter defaultFormatter = DateTimeFormatter.ISO_LOCAL_TIME; private final DateTimeFormatter formatter; public LocalTimeConverter() { this.formatter = defaultFormatter; } public LocalTimeConverter(DateTimeFormatter formatter) { this.formatter = formatter == null ? defaultFormatter : formatter; } @Override public void serialize(LocalTime object, ObjectWriter writer, Context ctx) throws Exception { writer.writeString(object.format(formatter)); } @Override public LocalTime deserialize(ObjectReader reader, Context ctx) throws Exception { String text = reader.valueAsString(); return text == null || text.isEmpty() ? null : LocalTime.parse(text, formatter); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy