com.github.nmorel.gwtjackson.client.ser.BaseDateJsonSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gwt-jackson Show documentation
Show all versions of gwt-jackson Show documentation
gwt-jackson is a GWT JSON serializer/deserializer mechanism based on Jackson annotations
package com.github.nmorel.gwtjackson.client.ser;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import com.github.nmorel.gwtjackson.client.JsonSerializationContext;
import com.github.nmorel.gwtjackson.client.JsonSerializer;
import com.github.nmorel.gwtjackson.client.stream.JsonWriter;
import com.github.nmorel.gwtjackson.client.utils.DateFormat;
/**
* Base implementation of {@link JsonSerializer} for dates.
*
* @author Nicolas Morel
*/
public abstract class BaseDateJsonSerializer extends JsonSerializer {
/**
* Default implementation of {@link BaseDateJsonSerializer} for {@link Date}
*/
public static final class DateJsonSerializer extends BaseDateJsonSerializer {
private static final DateJsonSerializer INSTANCE = new DateJsonSerializer();
/**
* @return an instance of {@link DateJsonSerializer}
*/
public static DateJsonSerializer getInstance() {
return INSTANCE;
}
private DateJsonSerializer() { }
@Override
protected void doSerialize( JsonWriter writer, @Nonnull Date value, JsonSerializationContext ctx ) throws IOException {
if ( ctx.isWriteDatesAsTimestamps() ) {
writer.value( value.getTime() );
} else {
writer.value( DateFormat.format( value ) );
}
}
}
/**
* Default implementation of {@link BaseDateJsonSerializer} for {@link java.sql.Date}
*/
public static final class SqlDateJsonSerializer extends BaseDateJsonSerializer {
private static final SqlDateJsonSerializer INSTANCE = new SqlDateJsonSerializer();
/**
* @return an instance of {@link SqlDateJsonSerializer}
*/
public static SqlDateJsonSerializer getInstance() {
return INSTANCE;
}
private SqlDateJsonSerializer() { }
@Override
protected void doSerialize( JsonWriter writer, @Nonnull java.sql.Date value, JsonSerializationContext ctx ) throws IOException {
writer.value( value.toString() );
}
}
private static final BaseDateJsonSerializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy