Refine search
private static <T extends JsonNode> T tryDecode(JsonNode encodedNode, T defaultValue, String fieldName) throws IOException { if (encodedNode == null || encodedNode.isNull()) { return defaultValue; } if (!encodedNode.isTextual()) { throw new IllegalArgumentException("error before decoding " + fieldName + "." + " Expected textual or null field, but encountered " + encodedNode.getNodeType()); } JsonNode decodedNode = mapper.readTree(encodedNode.textValue()); if (!decodedNode.getNodeType().equals(defaultValue.getNodeType())) { throw new IllegalArgumentException("error after decoding" + fieldName + "." + " Expected decoded type to be " + defaultValue.getNodeType() + ", but encountered " + decodedNode.getNodeType()); } return (T) decodedNode; }
@Override public PathDetail deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode pathDetail = jp.readValueAsTree(); if (pathDetail.size() != 3) throw new JsonParseException(jp, "PathDetail array must have exactly 3 entries but was " + pathDetail.size()); JsonNode from = pathDetail.get(0); JsonNode to = pathDetail.get(1); JsonNode val = pathDetail.get(2); PathDetail pd; if (val.isBoolean()) pd = new PathDetail(val.asBoolean()); else if (val.isLong()) pd = new PathDetail(val.asLong()); else if (val.isInt()) pd = new PathDetail(val.asInt()); else if (val.isDouble()) pd = new PathDetail(val.asDouble()); else if (val.isTextual()) pd = new PathDetail(val.asText()); else throw new JsonParseException(jp, "Unsupported type of PathDetail value " + pathDetail.getNodeType().name()); pd.setFirst(from.asInt()); pd.setLast(to.asInt()); return pd; } }
@Override public int hashCode() { return JsonNodeType.NULL.ordinal(); } }
static void convertJsonDocumentToMultiDimensionalCypherData(List<Object> data, DocumentKey key, JsonNode node, SpecificationElement element) { if (node.isNull()) { data.add(Collections.emptyList()); } else if (node.isContainerNode()) { ArrayList<Object> containerValue = new ArrayList<>(); data.add(containerValue); if (node.isObject()) { for (Map.Entry<String, SpecificationElement> entry : element.getProperties().entrySet()) { String fieldName = entry.getKey(); data.add(List.of(node.booleanValue(), node.booleanValue())); } else { throw new IllegalStateException("Got unsupported jackson-specific JsonNodeType " + node.getNodeType().name() + ", value: " + node.toString());
@Override public List<String> deserialize(JsonParser jp, DeserializationContext context) throws IOException { List<String> values = Collections.emptyList(); JsonNode rootNode = jp.getCodec().readTree(jp); JsonNode buckets = rootNode.get("aggregations").get("distinct_values").get("buckets"); if (JsonNodeType.ARRAY.equals(buckets.getNodeType())) { values = new ArrayList<>(buckets.size()); Iterator<JsonNode> iter = buckets.elements(); while (iter.hasNext()) { JsonNode bucket = iter.next(); String value = bucket.get("key").asText(); values.add(value); } } return values; } }
public BigDecimal getBigDecimal(String key, ObjectNode node, boolean required, String location, ParseResult result) { BigDecimal value = null; JsonNode v = node.get(key); if (node == null || v == null) { if (required) { result.missing(location, key); result.invalid(); } } else if(v.getNodeType().equals(JsonNodeType.NUMBER)) { value = new BigDecimal(v.asText()); } else if(!v.isValueNode()) { result.invalidType(location, key, "double", node); } return value; }
private void validateGELFMessage(JsonNode jsonNode, UUID id, ResolvableInetSocketAddress remoteAddress) { final String prefix = "GELF message <" + id + "> " + (remoteAddress == null ? "" : "(received from <" + remoteAddress + ">) "); final JsonNode hostNode = jsonNode.path("host"); if (hostNode.isMissingNode()) { log.warn(prefix + "is missing mandatory \"host\" field."); } else { if (!hostNode.isTextual()) { throw new IllegalArgumentException(prefix + "has invalid \"host\": " + hostNode.asText()); final JsonNode timestampNode = jsonNode.path("timestamp"); if (timestampNode.isValueNode() && !timestampNode.isNumber()) { log.warn(prefix + "has invalid \"timestamp\": {} (type: {})", timestampNode.asText(), timestampNode.getNodeType().name());
public Object getAnyExample(String nodeKey,ObjectNode node, String location, ParseResult result ){ JsonNode example = node.get(nodeKey); if (example != null) { if (example.getNodeType().equals(JsonNodeType.STRING)) { String value = getString(nodeKey, node, false, location, result); if (StringUtils.isNotBlank(value)) { return value; } if (example.getNodeType().equals(JsonNodeType.NUMBER)) { Integer integerExample = getInteger(nodeKey, node, false, location, result); if (integerExample != null) { } else if (example.getNodeType().equals(JsonNodeType.OBJECT)) { ObjectNode objectValue = getObject(nodeKey, node, false, location, result); if (objectValue != null) { return objectValue; } else if (example.getNodeType().equals(JsonNodeType.ARRAY)) { ArrayNode arrayValue = getArray(nodeKey, node, false, location, result); if (arrayValue != null) {
public Boolean getBoolean(String key, ObjectNode node, boolean required, String location, ParseResult result) { Boolean value = null; JsonNode v = node.get(key); if (node == null || v == null) { if (required) { result.missing(location, key); result.invalid(); } } else { if (v.getNodeType().equals(JsonNodeType.BOOLEAN)) { value = v.asBoolean(); } else if (v.getNodeType().equals(JsonNodeType.STRING)) { String stringValue = v.textValue(); return Boolean.parseBoolean(stringValue); } } return value; }
public static ObjectNode cleanProperties(ObjectMapper mapper, ObjectNode content) { ObjectNode cleaned = mapper.createObjectNode(); Iterator<Map.Entry<String, JsonNode>> fields = content.fields(); for ( ; fields.hasNext(); ) { Map.Entry<String, JsonNode> field = fields.next(); String fieldId = field.getKey(); if( field.getValue() != null && !field.getValue().getNodeType().equals(JsonNodeType.NULL)) { if( field.getValue().getNodeType().equals(JsonNodeType.OBJECT)) { ObjectNode clean = cleanProperties(mapper, field.getValue().deepCopy()); if( clean != null && clean.size() > 0 ) { cleaned.put(fieldId, clean); } } else if ( field.getValue().getNodeType().equals(JsonNodeType.ARRAY)) { ArrayNode clean = cleanArray((ArrayNode)field.getValue()); if( clean != null && clean.size() > 0 ) { cleaned.put(fieldId, clean); } } else if ( field.getValue().getNodeType().equals(JsonNodeType.STRING)) { String value = content.get(fieldId).asText(); if( value != null && StringUtils.isNotBlank(value)) { cleaned.put(fieldId, value); } } else { cleaned.put(fieldId, field.getValue()); } } } return cleaned; }
String nodeType = content.getNodeType().name().toLowerCase(Locale.ROOT); if (content.isContainerNode()) { return new StringContentProvider(contentType.or("application/json"), resolveSecrets(content, secrets).toString(), UTF_8); case "form": if (content.isArray()) {
public Integer getInteger(String key, ObjectNode node, boolean required, String location, ParseResult result) { Integer value = null; JsonNode v = node.get(key); if (node == null || v == null) { if (required) { result.missing(location, key); result.invalid(); } } else if(v.getNodeType().equals(JsonNodeType.NUMBER)) { value = v.intValue(); } else if(!v.isValueNode()) { result.invalidType(location, key, "integer", node); } return value; }
@Override public Date getDate(int columnIndex) throws SQLException { JsonNode ret = getColumnValue(columnIndex); if (wasNull) { return null; } if (ret instanceof NumericNode) { return new Date(ret.asLong()); } else if (ret instanceof TextNode) { return Date.valueOf(ret.asText()); } else { throw new SQLException("Date converted from " + ret.getNodeType().name() + " not supported"); } }
private List<JsonNode> getPublicEndpoints(JsonNode objectNode) { return objectNode.get("publicEndpoints").getNodeType().equals(JsonNodeType.ARRAY) ? convertJsonArrayToList((ArrayNode) objectNode.get("publicEndpoints")) : Collections.emptyList(); }
/** * merge parent and child properties maps. * @param content ObjectNode * @param parent ObjectNode * @return merged ObjectNode */ public static ObjectNode mergeProperties(ObjectNode content, ObjectNode parent) { ObjectNode merged = parent.deepCopy(); Iterator<Map.Entry<String, JsonNode>> fields = content.fields(); for ( ; fields.hasNext(); ) { Map.Entry<String, JsonNode> field = fields.next(); String fieldId = field.getKey(); if( merged.get(fieldId) != null ) { if( merged.get(fieldId).getNodeType().equals(JsonNodeType.OBJECT)) { merged.put(fieldId, mergeProperties(field.getValue().deepCopy(), (ObjectNode)merged.get(fieldId))); } else if ( merged.get(fieldId).getNodeType().equals(JsonNodeType.ARRAY)) { merged.put(fieldId, mergeArrays(((ArrayNode)field.getValue()), ((ArrayNode)merged.get(fieldId)))); } else { merged.put(fieldId, content.get(fieldId)); } } else { merged.put(fieldId, content.get(fieldId)); } } return merged; }
public List<String> tagStrings(ArrayNode nodes, String location, ParseResult result) { if(nodes == null) return null; List<String> output = new ArrayList<String>(); for(JsonNode node : nodes) { if(node.getNodeType().equals(JsonNodeType.STRING)) { output.add(node.textValue()); } } return output; }
public JsonQueryTypeException(final JsonNode obj1, final JsonNode obj2, final String msg) { super(String.format("%s (%s) and %s (%s) %s", obj1.getNodeType().toString().toLowerCase(), truncate(obj1.toString(), MAX_JSON_STRING_LENGTH), obj2.getNodeType().toString().toLowerCase(), truncate(obj2.toString(), MAX_JSON_STRING_LENGTH), msg)); } }
@Override public T valueNonNull(JsonNode node) { if (!node.isArray()) { throw new IllegalArgumentException("Node is not an array: " + node.getNodeType().name()); } T container = containerSupplier.get(); ArrayNode array = (ArrayNode) node; array.forEach(child -> container.add(elementConverter.value(child))); return container; } }
public Number getNumber(String key, ObjectNode node, boolean required, String location, ParseResult result) { Number value = null; JsonNode v = node.get(key); if (v == null) { if (required) { result.missing(location, key); result.invalid(); } } else if(v.getNodeType().equals(JsonNodeType.NUMBER)) { value = v.numberValue(); } else if(!v.isValueNode()) { result.invalidType(location, key, "number", node); } return value; }
public List<Tag> tags(ArrayNode nodes, String location, ParseResult result) { if(nodes == null) return null; List<Tag> output = new ArrayList<Tag>(); for(JsonNode node : nodes) { if(node.getNodeType().equals(JsonNodeType.OBJECT)) { Tag tag = tag((ObjectNode) node, location + ".tags", result); if(tag != null) { output.add(tag); } } } return output; }