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

com.github.dreamhead.moco.parser.deserializer.LatencyContainerDeserializer Maven / Gradle / Ivy

Go to download

Moco is an easy setup stub framework, mainly focusing on testing and integration.

There is a newer version: 1.5.0
Show newest version
package com.github.dreamhead.moco.parser.deserializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.dreamhead.moco.parser.model.LatencyContainer;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public final class LatencyContainerDeserializer extends JsonDeserializer {
    @Override
    public LatencyContainer deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
        JsonToken currentToken = jp.getCurrentToken();
        if (currentToken == JsonToken.VALUE_NUMBER_INT) {
            return LatencyContainer.latency(jp.getLongValue());
        }

        if (currentToken == JsonToken.START_OBJECT) {
            jp.nextToken();
            InternalLatencyContainer container = jp.readValueAs(InternalLatencyContainer.class);
            return container.toLatencyContainer();
        }

        return (LatencyContainer) ctxt.handleUnexpectedToken(LatencyContainer.class, jp);
    }

    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
    private static class InternalLatencyContainer {
        private long duration;
        private String unit;

        private LatencyContainer toLatencyContainer() {
            return LatencyContainer.latencyWithUnit(duration,
                    TimeUnit.valueOf(unit.toUpperCase() + 'S'));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy