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

io.lindstrom.mpd.support.DurationDeserializer Maven / Gradle / Ivy

There is a newer version: 0.13
Show newest version
package io.lindstrom.mpd.support;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import java.io.IOException;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.Date;

public class DurationDeserializer extends JsonDeserializer {
    @Override
    public Duration deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String text = p.getText();

        try {
            return Duration.parse(text);
        } catch (DateTimeParseException e) {
            // ignore
        }

        // Fallback to javax.xml.datatype
        try {
            return Duration.ofMillis(DatatypeFactory.newInstance().newDuration(text).getTimeInMillis(new Date(0L)));
        } catch (DatatypeConfigurationException e) {
            throw new IOException(e);
        } catch (Exception e) {
            ctxt.reportWrongTokenException(this, p.currentToken(), "Invalid duration");
        }

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy