org.kiwiproject.consul.util.SecondsDeserializer Maven / Gradle / Ivy
package org.kiwiproject.consul.util;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.util.regex.Pattern;
/**
* Deserializes Consul time values with "s" suffix to {@link Long} objects.
*
* @see Blocking Queries
*/
public class SecondsDeserializer extends JsonDeserializer {
private static final Pattern PATTERN = Pattern.compile("[a-zA-Z]");
@Override
public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
var durationString = p.getValueAsString();
if (isNotBlank(durationString)) {
var duration = PATTERN.matcher(durationString).replaceAll("");
return toLongOrThrow(duration);
}
return null;
}
private static Long toLongOrThrow(String value) {
try {
return Long.valueOf(value);
} catch (NumberFormatException e) {
throw new IllegalStateException("Expected a number but received a non-numeric value", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy