Created
February 18, 2014 05:26
-
-
Save sukrit007/9065086 to your computer and use it in GitHub Desktop.
Sample Implementation for UntypedJsonDataConverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.meltmedia.cdp.orch.util | |
import com.amazonaws.services.simpleworkflow.flow.DataConverterException | |
import com.amazonaws.services.simpleworkflow.flow.JsonDataConverter | |
import com.fasterxml.jackson.databind.JsonNode | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.databind.node.ArrayNode | |
import com.google.common.collect.Lists | |
import java.lang.reflect.Method | |
import java.lang.reflect.Type | |
/** | |
* JsonDataConverter for AWS FLow without using types. | |
*/ | |
class UntypedJsonDataConverter extends JsonDataConverter { | |
UntypedJsonDataConverter() { | |
super() | |
//Disable typing | |
this.mapper.disableDefaultTyping() | |
} | |
UntypedJsonDataConverter(ObjectMapper mapper) { | |
super(mapper) | |
//Disable typing | |
this.mapper.disableDefaultTyping() | |
} | |
@Override | |
Object[] parseMethodArguments(String serialized, Method method) throws DataConverterException { | |
JsonNode jsonNode = this.mapper.readValue(serialized, JsonNode) | |
if(!(jsonNode instanceof ArrayNode)){ | |
throw new DataConverterException("The JSON message is not an array") | |
} | |
ArrayNode arrayNode = (ArrayNode)jsonNode | |
if(method.genericParameterTypes.length != arrayNode.size()) { | |
throw new DataConverterException("The JSON message array size is expected to be: ${method.parameterTypes.length}." + | |
" Actual Size:${jsonNode.size()}") | |
} | |
List<Object> arguments = Lists.newArrayList() | |
method.genericParameterTypes.eachWithIndex{ Type type, int paramsCount -> | |
JsonNode itemNode = arrayNode.get(paramsCount) | |
arguments.add(mapper.convertValue(itemNode,mapper.constructType(type))) | |
} | |
arguments.toArray() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment