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

com.notronix.etsy.impl.common.json.InstantAdapter Maven / Gradle / Ivy

There is a newer version: 3.0.0014
Show newest version
package com.notronix.etsy.impl.common.json;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.time.Instant;

public class InstantAdapter extends TypeAdapter
{
    @Override
    public void write(JsonWriter out, Instant value) throws IOException {
        out.value(Math.floor((double) value.toEpochMilli() / 1000d));
    }

    @Override
    public Instant read(JsonReader in) throws IOException {
        JsonToken token = in.peek();

        if (token == JsonToken.NUMBER) {
            return Instant.ofEpochMilli(in.nextLong() * 1000);
        }

        throw new IOException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy