org.dominokit.jackson.deser.BaseDateJsonDeserializer Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2013 Nicolas Morel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.jackson.deser;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import org.dominokit.jackson.JacksonContextProvider;
import org.dominokit.jackson.JsonDeserializationContext;
import org.dominokit.jackson.JsonDeserializer;
import org.dominokit.jackson.JsonDeserializerParameters;
import org.dominokit.jackson.stream.JsonReader;
import org.dominokit.jackson.stream.JsonToken;
/** Base implementation of {@link org.dominokit.jackson.JsonDeserializer} for dates. */
public abstract class BaseDateJsonDeserializer extends JsonDeserializer {
/** Default implementation of {@link BaseDateJsonDeserializer} for {@link Date} */
public static final class DateJsonDeserializer extends BaseDateJsonDeserializer {
private static final DateJsonDeserializer INSTANCE = new DateJsonDeserializer();
/** @return an instance of {@link DateJsonDeserializer} */
public static DateJsonDeserializer getInstance() {
return INSTANCE;
}
private DateJsonDeserializer() {}
@Override
protected Date deserializeNumber(long millis, JsonDeserializerParameters params) {
return new Date(millis);
}
@Override
protected Date deserializeString(
String date, JsonDeserializationContext ctx, JsonDeserializerParameters params) {
return JacksonContextProvider.get()
.dateFormat()
.parse(ctx.isUseBrowserTimezone(), params.getPattern(), null, date);
}
}
/** Default implementation of {@link BaseDateJsonDeserializer} for {@link java.sql.Date} */
public static final class SqlDateJsonDeserializer
extends BaseDateJsonDeserializer {
private static final SqlDateJsonDeserializer INSTANCE = new SqlDateJsonDeserializer();
private static final String SQL_DATE_FORMAT = "yyyy-MM-dd";
/** @return an instance of {@link SqlDateJsonDeserializer} */
public static SqlDateJsonDeserializer getInstance() {
return INSTANCE;
}
private SqlDateJsonDeserializer() {}
@Override
protected java.sql.Date deserializeNumber(long millis, JsonDeserializerParameters params) {
return new java.sql.Date(millis);
}
@Override
protected java.sql.Date deserializeString(
String date, JsonDeserializationContext ctx, JsonDeserializerParameters params) {
return new java.sql.Date(
JacksonContextProvider.get()
.dateFormat()
.parse(ctx.isUseBrowserTimezone(), SQL_DATE_FORMAT, false, date)
.getTime());
}
}
/** Default implementation of {@link BaseDateJsonDeserializer} for {@link Time} */
public static final class SqlTimeJsonDeserializer extends BaseDateJsonDeserializer