List of usage examples for com.fasterxml.jackson.databind.node ArrayNode get
public JsonNode get(String paramString)
From source file:ru.histone.deparser.Deparser.java
protected String processMap(ArrayNode ast) { ArrayNode map = (ArrayNode) ast.get(1); Set<String> entriesAsStrings = new LinkedHashSet<String>(); for (JsonNode entry : map) { if (entry.isArray()) { ArrayNode entryAsArray = (ArrayNode) entry; JsonNode key = entryAsArray.get(0); JsonNode value = entryAsArray.get(1); String valueProcessed = processAstNode(value); if (!key.isNull()) { String keyAsString = key.asText(); entriesAsStrings.add(keyAsString + " : " + valueProcessed); } else { entriesAsStrings.add(valueProcessed); }/*from w w w. ja va 2 s . c o m*/ } } return "[" + StringUtils.join(entriesAsStrings, ", ") + "]"; }
From source file:ru.histone.deparser.Deparser.java
protected String processIf(ArrayNode ast) { ArrayNode ifBlock = (ArrayNode) ast.get(1); StringBuilder sb = new StringBuilder(); for (JsonNode ifElement : ifBlock) { JsonNode expression = ifElement.get(0); JsonNode statements = ifElement.get(1); String expressionProcessed = processAstNode(expression); if (!expressionProcessed.startsWith("(")) expressionProcessed = "(" + expressionProcessed; if (!expressionProcessed.endsWith(")")) expressionProcessed = expressionProcessed + ")"; sb.append(ind() + "if " + expressionProcessed + " {\n"); indent();//from w w w.j a v a 2 s.c o m for (JsonNode statement : statements) { String s = processAstNode(statement); if (s != null && !StringUtils.isEmpty(s)) sb.append(ind() + s + "\n"); } unindent(); sb.append(ind() + "}\n"); } return sb.toString(); }
From source file:ru.histone.deparser.Deparser.java
protected String processFor(ArrayNode ast) { ArrayNode var = (ArrayNode) ast.get(1); ArrayNode collection = (ArrayNode) ast.get(2); ArrayNode ifStatements = (ArrayNode) ast.get(3).get(0); ArrayNode elseStatements = (ast.get(3).size() > 1) ? elseStatements = (ArrayNode) ast.get(3).get(1) : null; StringBuilder result = new StringBuilder(); String v1 = var.get(0).asText(); String v2 = (var.size() > 1) ? var.get(1).asText() : null; String collectionProcessed = processAstNode(collection); result.append(ind() + "for (" + v1 + "," + v2 + " in " + collectionProcessed + ") {\n"); indent();//w ww. j av a 2s . c o m for (JsonNode ifStatement : ifStatements) { String s = processAstNode(ifStatement); if (s != null) result.append(s); } unindent(); if (elseStatements != null) { result.append(ind() + "} else {\n"); for (JsonNode elseStatement : elseStatements) { String s = processAstNode(elseStatement); if (s != null) result.append(s); } result.append(ind() + "}\n"); } return result.toString(); }
From source file:ru.histone.deparser.Deparser.java
protected String processBinaryOperation(ArrayNode ast) { int opType = abs(ast.get(0).asInt()); String arg1 = processAstNode(ast.get(1)); String arg2 = processAstNode(ast.get(2)); if (opType == AstNodeType.ADD) { return "(" + arg1 + " + " + arg2 + ")"; }/*from ww w .j av a2s .com*/ if (opType == AstNodeType.SUB) { return "(" + arg1 + " - " + arg2 + ")"; } if (opType == AstNodeType.MUL) { return "(" + arg1 + " * " + arg2 + ")"; } if (opType == AstNodeType.DIV) { return "(" + arg1 + " / " + arg2 + ")"; } if (opType == AstNodeType.MOD) { return "(" + arg1 + " % " + arg2 + ")"; } if (opType == AstNodeType.OR) { return "(" + arg1 + " || " + arg2 + ")"; } if (opType == AstNodeType.AND) { return "(" + arg1 + " && " + arg2 + ")"; } if (opType == AstNodeType.EQUAL) { return "(" + arg1 + " == " + arg2 + ")"; } if (opType == AstNodeType.NOT_EQUAL) { return "(" + arg1 + " != " + arg2 + ")"; } if (opType == AstNodeType.LESS_OR_EQUAL) { return "(" + arg1 + " <= " + arg2 + ")"; } if (opType == AstNodeType.LESS_THAN) { return "(" + arg1 + " < " + arg2 + ")"; } if (opType == AstNodeType.GREATER_OR_EQUAL) { return "(" + arg1 + " >= " + arg2 + ")"; } if (opType == AstNodeType.GREATER_THAN) { return "(" + arg1 + " > " + arg2 + ")"; } return null; }
From source file:org.pentaho.metaverse.impl.model.kettle.json.TransMetaJsonDeserializer.java
protected void deserializeConnections(TransMeta transMeta, JsonNode node, ObjectMapper mapper) { ArrayNode connectionsArrayNode = (ArrayNode) node.get(TransMetaJsonSerializer.JSON_PROPERTY_CONNECTIONS); IExternalResourceInfo conn = null;/*from w ww . j a va 2 s.c om*/ for (int i = 0; i < connectionsArrayNode.size(); i++) { JsonNode connNode = connectionsArrayNode.get(i); String className = connNode.get(IInfo.JSON_PROPERTY_CLASS).asText(); try { Class clazz = this.getClass().getClassLoader().loadClass(className); conn = (IExternalResourceInfo) clazz.newInstance(); conn = mapper.readValue(connNode.toString(), conn.getClass()); DatabaseMeta dbMeta = null; if (conn instanceof JdbcResourceInfo) { JdbcResourceInfo db = (JdbcResourceInfo) conn; dbMeta = new DatabaseMeta(db.getName(), db.getPluginId(), DatabaseMeta.getAccessTypeDesc(DatabaseMeta.TYPE_ACCESS_NATIVE), db.getServer(), db.getDatabaseName(), String.valueOf(db.getPort()), db.getUsername(), db.getPassword()); } else if (conn instanceof JndiResourceInfo) { JndiResourceInfo db = (JndiResourceInfo) conn; dbMeta = new DatabaseMeta(db.getName(), db.getPluginId(), DatabaseMeta.getAccessTypeDesc(DatabaseMeta.TYPE_ACCESS_JNDI), null, null, null, null, null); } transMeta.addDatabase(dbMeta); } catch (Exception e) { LOGGER.warn(Messages.getString("WARNING.Deserialization.Trans.Connections", conn.getName(), transMeta.getName()), e); } } }
From source file:io.cloudslang.content.json.actions.GetValueFromObject.java
private JsonNode getValue(JsonNode jsonElement, String aKey) throws Exception { //non JSON array object if (!aKey.matches(".*" + ESCAPED_SLASH + "[[0-9]+]$")) { if (jsonElement.get(aKey) != null) { return jsonElement.get(aKey); } else/*from w w w . j a v a 2 s . c o m*/ throw new Exception("The " + aKey + " key does not exist in JavaScript object!"); } //JSON array object else { int startIndex = aKey.indexOf("["); int endIndex = aKey.indexOf("]"); String oneKey = aKey.substring(0, startIndex); int index = 0; try { index = Integer.parseInt(aKey.substring(startIndex + 1, endIndex)); } catch (NumberFormatException e) { throw new Exception("Invalid index provided: " + index); } JsonNode subObject; subObject = jsonElement.get(oneKey); if (jsonElement.get(oneKey) != null) { if (subObject instanceof ArrayNode) { final ArrayNode asJsonArray = (ArrayNode) subObject; if ((index >= asJsonArray.size()) || (index < 0)) { throw new Exception("The provided " + index + " index is out of range! Provide a valid index value in the provided JSON!"); } else { return asJsonArray.get(index); } } else { throw new Exception("Invalid json array provided: " + subObject.toString() + " "); } } else { throw new Exception("The " + aKey + " key does not exist in JavaScript object!"); } } }
From source file:org.gitana.platform.client.api.ClientImpl.java
@Override public Collection<String> getAuthorizedGrantTypes() { List<String> authorizedGrantTypes = new ArrayList<String>(); ArrayNode array = getArray(FIELD_AUTHORIZED_GRANT_TYPES); if (array != null) { for (int i = 0; i < array.size(); i++) { String authorizedGrantType = array.get(i).textValue(); authorizedGrantTypes.add(authorizedGrantType); }//from ww w.ja v a 2s . c o m } return authorizedGrantTypes; }
From source file:org.envirocar.server.rest.util.GeoJSON.java
@Override public MultiLineString decodeMultiLineString(JsonNode json) throws GeometryConverterException { ArrayNode coordinates = requireCoordinates(json); LineString[] lineStrings = new LineString[coordinates.size()]; for (int i = 0; i < coordinates.size(); ++i) { Object coords = coordinates.get(i); lineStrings[i] = getGeometryFactory().createLineString(decodeCoordinates(toList(coords))); }/*from w ww. j a v a2 s . c o m*/ return getGeometryFactory().createMultiLineString(lineStrings); }
From source file:org.envirocar.server.rest.util.GeoJSON.java
protected Coordinate decodeCoordinate(ArrayNode list) throws GeometryConverterException { if (list.size() != 2) { throw new GeometryConverterException("coordinates may only have 2 dimensions"); }/*from w w w. j a va 2 s. co m*/ Number x = list.get(0).numberValue(); Number y = list.get(1).numberValue(); if (x == null || y == null) { throw new GeometryConverterException("x and y have to be numbers"); } return new Coordinate(x.doubleValue(), y.doubleValue()); }
From source file:com.squarespace.template.plugins.platform.CommerceUtils.java
public static ArrayNode getItemVariantOptions(JsonNode item) { JsonNode structuredContent = item.path("structuredContent"); JsonNode variants = structuredContent.path("variants"); if (variants.size() <= 1) { return EMPTY_ARRAY; }/*from w w w.j a va 2s. c o m*/ ArrayNode userDefinedOptions = JsonUtils.createArrayNode(); JsonNode ordering = structuredContent.path("variantOptionOrdering"); for (int i = 0; i < ordering.size(); i++) { String optionName = ordering.path(i).asText(); ObjectNode option = JsonUtils.createObjectNode(); option.put("name", optionName); option.put("values", JsonUtils.createArrayNode()); userDefinedOptions.add(option); } for (int i = 0; i < variants.size(); i++) { JsonNode variant = variants.path(i); JsonNode attributes = variant.get("attributes"); if (attributes == null) { continue; } Iterator<String> fields = attributes.fieldNames(); while (fields.hasNext()) { String field = fields.next(); String variantOptionValue = attributes.get(field).asText(); ObjectNode userDefinedOption = null; for (int j = 0; j < userDefinedOptions.size(); j++) { ObjectNode current = (ObjectNode) userDefinedOptions.get(j); if (current.get("name").asText().equals(field)) { userDefinedOption = current; } } if (userDefinedOption != null) { boolean hasOptionValue = false; ArrayNode optionValues = (ArrayNode) userDefinedOption.get("values"); for (int k = 0; k < optionValues.size(); k++) { String optionValue = optionValues.get(k).asText(); if (optionValue.equals(variantOptionValue)) { hasOptionValue = true; break; } } if (!hasOptionValue) { optionValues.add(variantOptionValue); } } } } return userDefinedOptions; }