private static Object convert(JsonNode value) { if (value.isArray()) { List<String> retvalList = new ArrayList<>(); for (JsonNode arrayElement : value) retvalList.add(arrayElement.asText()); return retvalList; } return value.getNodeType() == JsonNodeType.NUMBER ? value.numberValue() : value.asText(); }
private String getTypeName(JsonNode node) { if (node.has("type") && node.get("type").isArray() && node.get("type").size() > 0) { for (JsonNode jsonNode : node.get("type")) { String typeName = jsonNode.asText(); if (!typeName.equals("null")) { return typeName; } } } if (node.has("type") && node.get("type").isTextual()) { return node.get("type").asText(); } return DEFAULT_TYPE_NAME; }
private List<String> getColumnMetadata(Optional<String> parent, JsonNode propertiesNode) { ImmutableList.Builder<String> metadata = ImmutableList.builder(); Iterator<Entry<String, JsonNode>> iterator = propertiesNode.fields(); while (iterator.hasNext()) { Entry<String, JsonNode> entry = iterator.next(); String key = entry.getKey(); JsonNode value = entry.getValue(); String childKey; if (parent.isPresent()) { if (parent.get().isEmpty()) { childKey = key; } else { childKey = parent.get().concat(".").concat(key); } } else { childKey = key; } if (value.isObject()) { metadata.addAll(getColumnMetadata(Optional.of(childKey), value)); continue; } if (!value.isArray()) { metadata.add(childKey.concat(":").concat(value.textValue())); } } return metadata.build(); }
protected List<String> getActiveValueList(List<String> originalValues, String propertyName, ObjectNode taskElementProperties) { List<String> activeValues = originalValues; if (taskElementProperties != null) { JsonNode overrideValuesNode = taskElementProperties.get(propertyName); if (overrideValuesNode != null) { if (overrideValuesNode.isNull() || !overrideValuesNode.isArray() || overrideValuesNode.size() == 0) { activeValues = null; } else { activeValues = new ArrayList<String>(); for (JsonNode valueNode : overrideValuesNode) { activeValues.add(valueNode.asText()); } } } } return activeValues; } }
private List<String> getTextProps(String key, Map<String,JsonNode> props, Token token) throws ParseException { JsonNode value = props.get(key); if (!value.isArray()) throw error(key+" property must be array: "+value, token); List<String> values = new ArrayList<String>(); for (JsonNode n : value) if (n.isTextual()) values.add(n.textValue()); else throw error(key+" values must be textual: "+n, token); return values; }
private void flattenElement(String propertyPrefix, Object source, Map<String, Object> resultMap) { if (!(source instanceof JsonNode)) { resultMap.put(propertyPrefix, source); return; } JsonNode element = (JsonNode) source; if (element.isArray()) { Iterator<JsonNode> nodes = element.elements(); while (nodes.hasNext()) { JsonNode cur = nodes.next(); if (cur.isArray()) { this.falttenCollection(propertyPrefix, cur.elements(), resultMap); } } } else if (element.isContainerNode()) { this.doFlatten(propertyPrefix, element.fields(), resultMap); } else { resultMap.put(propertyPrefix, new DirectFieldAccessFallbackBeanWrapper(element).getPropertyValue("_value")); } }
@Override public <T> List<T> asList(Class<T> tClazz) throws JWTDecodeException { if (!data.isArray()) { return null; } List<T> list = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { try { list.add(objectReader.treeToValue(data.get(i), tClazz)); } catch (JsonProcessingException e) { throw new JWTDecodeException("Couldn't map the Claim's array contents to " + tClazz.getSimpleName(), e); } } return list; }
@Override public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); if (node.isTextual()) { return node.textValue(); } else if (node.isObject()) { JsonNode allNode = node.get("__all__"); if (allNode != null && allNode.isArray()) { StringBuffer buf = new StringBuffer(); for (JsonNode msgNode : allNode) { buf.append(msgNode.textValue()); buf.append(","); } return buf.length() > 0 ? buf.substring(0, buf.length() - 1) : buf.toString(); } return node.toString(); } return null; } }
private JsonNode resolve(JsonNode tree, List<String> path) { if (path.isEmpty()) { return tree; } else { String part = path.remove(0); if (tree.isArray()) { try { int index = Integer.parseInt(part); return resolve(tree.get(index), path); } catch (NumberFormatException e) { throw new IllegalArgumentException("Not a valid array index: " + part); } } if (tree.has(part)) { return resolve(tree.get(part), path); } else { throw new IllegalArgumentException("Path not present: " + part); } } } }
@Override public BitZPublicOrder deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec oc = p.getCodec(); JsonNode node = oc.readTree(p); if (node.isArray()) { BigDecimal price = new BigDecimal(node.path(0).asText()); BigDecimal volume = new BigDecimal(node.path(1).asText()); return new BitZPublicOrder(price, volume); } return null; } }
private Object extractBody(ClientResponse cr) { String json = cr.getEntity(String.class); logger.info(json); try { JsonNode node = om.readTree(json); if (node.isArray()) { return om.convertValue(node, listOfObj); } else if (node.isObject()) { return om.convertValue(node, mapOfObj); } else if (node.isNumber()) { return om.convertValue(node, Double.class); } else { return node.asText(); } } catch (IOException jpe) { logger.error(jpe.getMessage(), jpe); return json; } }
@Override public RangeBoundValue deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode node = jp.getCodec().readTree(jp); if (node.isNull()) { return null; } else { List<Object> list; if (node.isArray()) { list = new ArrayList<>(); Iterator<JsonNode> iter = node.elements(); while (iter.hasNext()) { Object v = toValue(iter.next()); list.add(v); } } else { Object v = toValue(node); list = ImmutableList.of(v); } return new RangeBoundValue(list); } }
List<String> getStringOrArray(Map<String, JsonNode> tree, String claimName) throws JWTDecodeException { JsonNode node = tree.get(claimName); if (node == null || node.isNull() || !(node.isArray() || node.isTextual())) { return null; } if (node.isTextual() && !node.asText().isEmpty()) { return Collections.singletonList(node.asText()); } List<String> list = new ArrayList<>(node.size()); for (int i = 0; i < node.size(); i++) { try { list.add(objectReader.treeToValue(node.get(i), String.class)); } catch (JsonProcessingException e) { throw new JWTDecodeException("Couldn't map the Claim's array contents to String", e); } } return list; }
/** * Generates a list of actions from the Action Json Node. * * @param actionNodes * the action Json node to be parsed. * @return the list of actions. */ private List<Action> actionsOf(JsonNode actionNodes) { List<Action> actions = new LinkedList<Action>(); if (actionNodes.isArray()) { for (JsonNode action : actionNodes) { actions.add(new NamedAction(action.asText())); } } else { actions.add(new NamedAction(actionNodes.asText())); } return actions; }
@Override protected GeoJsonMultiLineString doDeserialize(ArrayNode coordinates) { List<GeoJsonLineString> lines = new ArrayList<GeoJsonLineString>(coordinates.size()); for (JsonNode lineString : coordinates) { if (lineString.isArray()) { lines.add(toLineString((ArrayNode) lineString)); } } return new GeoJsonMultiLineString(lines); } }
public JsonNode visit(JmesPathFlatten flatten, JsonNode input) throws InvalidTypeException { JsonNode flattenResult = flatten.getFlattenExpr().accept(this, input); if (flattenResult.isArray()) { Iterator<JsonNode> elements = flattenResult.elements(); ArrayNode flattenedArray = ObjectMapperSingleton.getObjectMapper().createArrayNode(); while (elements.hasNext()) { JsonNode element = elements.next(); if (element != null) { if (element.isArray()) { Iterator<JsonNode> inner = element.iterator(); while (inner.hasNext()) { JsonNode innerElement = inner.next(); if (innerElement != null) { flattenedArray.add(innerElement);
@SuppressWarnings("unchecked") public static List<List<?>> getValues(BeakerObjectConverter parent, JsonNode n, ObjectMapper mapper) throws IOException { List<List<?>> values = null; List<String> classes = null; if (n.has("types")) classes = mapper.readValue(n.get("types").toString(), List.class); if (n.has("values")) { JsonNode nn = n.get("values"); values = new ArrayList<List<?>>(); if (nn.isArray()) { for (JsonNode nno : nn) { if (nno.isArray()) { ArrayList<Object> val = new ArrayList<Object>(); for (int i = 0; i < nno.size(); i++) { JsonNode nnoo = nno.get(i); Object obj = parent.deserialize(nnoo, mapper); val.add(TableDisplayDeSerializer.getValueForDeserializer(obj, classes != null && classes.size() > i ? classes.get(i) : null)); } values.add(val); } } } } return values; }
@Override public KucoinActiveOrder deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode root = p.readValueAsTree(); if (root.isArray()) { Date timestamp = new Date(root.get(0).asLong()); KucoinOrderType orderType = KucoinOrderType.valueOf(root.get(1).asText()); BigDecimal price = root.get(2).decimalValue(); BigDecimal amount = root.get(3).decimalValue(); BigDecimal dealAmount = root.get(4).decimalValue(); // amount already filled String orderOid = root.get(5).textValue(); return new KucoinActiveOrder(timestamp, orderType, price, amount, dealAmount, orderOid); } else { throw new RuntimeException("KucoinDealOrder should have an array as root node!"); } } }
@Override public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); if (node.isTextual()) { return node.textValue(); } else if (node.isObject()) { JsonNode allNode = node.get("__all__"); if (allNode != null && allNode.isArray()) { StringBuffer buf = new StringBuffer(); for (JsonNode msgNode : allNode) { buf.append(msgNode.textValue()); buf.append(","); } return buf.length() > 0 ? buf.substring(0, buf.length() - 1) : buf.toString(); } return node.toString(); } return null; } }