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

uk.co.mruoc.camunda.client.variable.VariablesDeserializer Maven / Gradle / Ivy

The newest version!
package uk.co.mruoc.camunda.client.variable;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

public class VariablesDeserializer extends StdDeserializer {

    protected VariablesDeserializer() {
        super(Variables.class);
    }

    @Override
    public Variables deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        JsonNode node = parser.getCodec().readTree(parser);
        Collection variables = toCollection(node.fields());
        return new Variables(variables);
    }

    private static Collection toCollection(Iterator> variableFields) {
        Collection variables = new ArrayList<>();
        variableFields.forEachRemaining(field -> variables.add(toVariable(field)));
        return Collections.unmodifiableCollection(variables);
    }

    private static Variable toVariable(Map.Entry field) {
        return toVariable(
                field.getValue().get("type"), field.getKey(), field.getValue().get("value"));
    }

    private static Variable toVariable(JsonNode typeNode, String name, JsonNode value) {
        String type = typeNode.asText();
        switch (type.toLowerCase()) {
            case "json":
                return new JsonVariable(name, value.asText());
            case "string":
                return new StringVariable(name, value.asText());
            case "long":
                return new LongVariable(name, value.asLong());
            case "boolean":
                return new BooleanVariable(name, value.asBoolean());
            default:
                throw new VariableTypeUnsupportedException(type);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy