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

website.automate.waml.io.deserializer.ActionDeserializer Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
package website.automate.waml.io.deserializer;

import static java.text.MessageFormat.format;
import static java.util.Collections.singletonMap;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;

import website.automate.waml.io.model.ActionType;
import website.automate.waml.io.model.CriterionType;
import website.automate.waml.io.model.action.Action;
import website.automate.waml.io.model.action.StoreAction;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class ActionDeserializer extends StdDeserializer {

    private static final long serialVersionUID = 8037140456765531389L;

    public ActionDeserializer() {
        super(Action.class);
    }

    @Override
    public Action deserialize(JsonParser jsonParser,
            DeserializationContext deserializationContext) throws IOException,
            JsonProcessingException {
        ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
        ObjectNode root = (ObjectNode) mapper.readTree(jsonParser);

        Class actionClass = null;
        Iterator> elementsIterator = root.fields();
        String key = null;
        while (elementsIterator.hasNext()) {
            Entry element = elementsIterator.next();
            String name = element.getKey();
            if(elementsIterator.hasNext()){
                String anotherName = elementsIterator.next().getKey();
                throw new TooManyActionsException(format("Single action expected, but found at least: {0}, {1}", name, anotherName));
            }
            actionClass = ActionType.findByName(name).getClazz();
            key = name;
        }

        if (actionClass == null){
            throw new UnknownActionException(format("Action {0} is unknown.", key));
        }
        
        JsonNode object = root.get(key);
        if (isShortNotated(actionClass, object)) {
            ActionType actionType = ActionType.findByName(key);
            JsonNode wrapper = new ObjectNode(JsonNodeFactory.instance,
                    singletonMap(actionType.getDefaultCriteriaType().getName(),
                            object));
            return mapper.convertValue(wrapper, actionClass);
        }
        
        return mapper.convertValue(root.get(key), actionClass);
    }
    
    private boolean isShortNotated(Class actionClass, JsonNode object){
        if(object.isTextual()){
            return true;
        }
        if(actionClass == StoreAction.class){
            return !object.has(CriterionType.IF.getName())
                    && !object.has(CriterionType.UNLESS.getName())
                    && !object.has(CriterionType.VALUE.getName());
        }
        return !object.isObject();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy