public Object unwrap(Object o) { if (o == null) { return null; } if (!(o instanceof JsonNode)) { return o; } JsonNode e = (JsonNode) o; if (e.isValueNode()) { if (e.isTextual()) { return e.asText(); } else if (e.isBoolean()) { return e.asBoolean(); } else if (e.isInt()) { return e.asInt(); } else if (e.isLong()) { return e.asLong(); } else if (e.isBigDecimal()) { return e.decimalValue(); } else if (e.isDouble()) { return e.doubleValue(); } else if (e.isFloat()) { return e.floatValue(); } else if (e.isBigDecimal()) { return e.decimalValue(); } else if (e.isNull()) { return null; } } return o; }
/** * This operation accepts an object in the JavaScript Object Notation format (JSON) and returns a * value for the specified key. * * @param object The string representation of a JSON object. Objects in JSON are a collection of * name value pairs, separated by a colon and surrounded with curly brackets {}. The name must * be a string value, and the value can be a single string or any valid JSON object or array. * Examples: {"one":1, "two":2}, {"one":{"a":"a","B":"B"}, "two":"two", "three":[1,2,3.4]} * @param key The key in the object to get the value of. Examples: city, location[0].city * @return a map containing the output of the operation. Keys present in the map are: * <p><br> * <br> * <b>returnResult</b> - This will contain the value for the specified key in the object. <br> * <b>exception</b> - In case of success response, this result is empty. In case of failure * response, this result contains the java stack trace of the runtime exception. <br> * <br> * <b>returnCode</b> - The returnCode of the operation: 0 for success, -1 for failure. */ @Action( name = "Get Value from Object", outputs = { @Output(Constants.OutputNames.RETURN_RESULT), @Output(Constants.OutputNames.RETURN_CODE), @Output(Constants.OutputNames.EXCEPTION) }, responses = { @Response( text = Constants.ResponseNames.SUCCESS, field = Constants.OutputNames.RETURN_CODE, value = Constants.ReturnCodes.RETURN_CODE_SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response( text = Constants.ResponseNames.FAILURE, field = Constants.OutputNames.RETURN_CODE, value = Constants.ReturnCodes.RETURN_CODE_FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) public Map<String, String> execute( @Param(value = Constants.InputNames.OBJECT, required = true) String object, @Param(value = Constants.InputNames.KEY, required = true) String key) { Map<String, String> returnResult = new HashMap<>(); if (isBlank(object)) { return populateResult(returnResult, new Exception("Empty object provided!")); } if (key == null) { return populateResult(returnResult, new Exception("Null key provided!")); } final JsonNode jsonRoot; ObjectMapper objectMapper = new ObjectMapper(); try { jsonRoot = objectMapper.readTree(object); } catch (Exception exception) { final String value = "Invalid object provided! " + exception.getMessage(); return populateResult(returnResult, value, exception); } int startIndex = 0; final JsonNode valueFromObject; try { valueFromObject = getObject(jsonRoot, key.split(ESCAPED_SLASH + "."), startIndex); } catch (Exception exception) { return populateResult(returnResult, exception); } if (valueFromObject.isValueNode()) { return populateResult(returnResult, valueFromObject.asText(), null); } else { return populateResult(returnResult, valueFromObject.toString(), null); } }
@Override public KeyValuedColumnType fromJsonNode(JsonNode json) { if (json.isValueNode() || !json.has("value")) { return null; } BaseType keyType = BaseType.fromJson(json, "key"); BaseType valueType = BaseType.fromJson(json, "value"); KeyValuedColumnType keyValueColumnType = new KeyValuedColumnType(keyType, valueType); JsonNode node = null; if ((node = json.get("min")) != null) { keyValueColumnType.setMin(node.asLong()); } if ((node = json.get("max")) != null) { if (node.isLong()) { keyValueColumnType.setMax(node.asLong()); } else if (node.isTextual() && "unlimited".equals(node.asText())) { keyValueColumnType.setMax(Long.MAX_VALUE); } } return keyValueColumnType; }