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

io.gsonfire.DateSerializationPolicy Maven / Gradle / Ivy

Go to download

A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getter) serialization, pre and post processors and many more. Check out the documentation to learn how to use it!

There is a newer version: 1.9.0
Show newest version
package io.gsonfire;

import io.gsonfire.gson.DateUnixtimeSecondsTypeAdapter;
import io.gsonfire.gson.DateUnixtimeMillisTypeAdapter;
import io.gsonfire.gson.DateRFC3339TypeAdapter;
import io.gsonfire.gson.NullableTypeAdapter;
import com.google.gson.TypeAdapter;

import java.util.Date;
import java.util.TimeZone;

/**
 * @autor: julio
 */
public enum DateSerializationPolicy {

    /**
     * Serializes to/from unix timestamps in milliseconds and allows negative numbers
     */
    unixTimeMillis {
        @Override
        TypeAdapter createTypeAdapter(TimeZone serializeTimezone) {
            return new NullableTypeAdapter(
                new DateUnixtimeMillisTypeAdapter(true)
            );
        }
    },

    /**
     * Serializes to/from unix timestamps in seconds and allows negative numbers
     */
    unixTimeSeconds {
        @Override
        TypeAdapter createTypeAdapter(TimeZone serializeTimezone) {
            return new NullableTypeAdapter(
                new DateUnixtimeSecondsTypeAdapter(true)
            );
        }
    },

    /**
     * Serializes to/from unix timestamps in milliseconds and only allows positive numbers. If a negative unix timestamp is passed, it
     * will be serialized as null
     */
    unixTimePositiveMillis {
        @Override
        TypeAdapter createTypeAdapter(TimeZone serializeTimezone) {
            return new NullableTypeAdapter(
                new DateUnixtimeMillisTypeAdapter(false)
            );
        }
    },

    /**
     * Serializes to/from unix timestamps and only allows positive numbers. If a negative unix timestamp is passed, it
     * will be serialized as null
     */
    unixTimePositiveSeconds {
        @Override
        TypeAdapter createTypeAdapter(TimeZone serializeTimezone) {
            return new NullableTypeAdapter(
                new DateUnixtimeSecondsTypeAdapter(false)
            );
        }
    },

    /**
     * Serializes dates in RFC3339 including the date and time. For example: 1985-04-12T23:20:50.52Z
     */
    rfc3339 {
        @Override
        TypeAdapter createTypeAdapter(TimeZone serializeTimezone) {
            return new NullableTypeAdapter(
                new DateRFC3339TypeAdapter(serializeTimezone, true)
            );
        }
    },

    /**
     * Serializes dates in RFC3339 including only date date. For example: 1985-04-12
     */
    rfc3339Date {
        @Override
        TypeAdapter createTypeAdapter(TimeZone serializeTimezone) {
            return new NullableTypeAdapter(
                new DateRFC3339TypeAdapter(serializeTimezone, false)
            );
        }
    };

    abstract TypeAdapter createTypeAdapter(TimeZone serializeTimezone);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy