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

org.dominokit.jacksonapt.deser.BaseDateJsonDeserializer Maven / Gradle / Ivy

/*
 * 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.jacksonapt.deser;

import org.dominokit.jacksonapt.JacksonContextProvider;
import org.dominokit.jacksonapt.JsonDeserializationContext;
import org.dominokit.jacksonapt.JsonDeserializer;
import org.dominokit.jacksonapt.JsonDeserializerParameters;
import org.dominokit.jacksonapt.stream.JsonReader;
import org.dominokit.jacksonapt.stream.JsonToken;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Base implementation of {@link org.dominokit.jacksonapt.JsonDeserializer} for dates.
 *
 * @author Nicolas Morel
 * @version $Id: $
 */
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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy