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

io.lindstrom.mpd.support.FrameRateDeserializer 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 io.lindstrom.mpd.data.FrameRate;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FrameRateDeserializer extends JsonDeserializer {
    private static final Pattern FRAME_RATE_PATTERN = Pattern.compile("^([0-9]+)(/[0-9]+)?$");

    @Override
    public FrameRate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String text = p.getText();
        Matcher matcher = FRAME_RATE_PATTERN.matcher(text);
        if (matcher.matches()) {
            String a = matcher.group(1);
            String b = matcher.group(2);
            return new FrameRate(Long.parseLong(a),
                    b == null ? null : Long.parseLong(b.substring(1)));
        } else {
            ctxt.reportWrongTokenException(this, p.currentToken(), "Invalid ratio");
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy