List of usage examples for com.fasterxml.jackson.databind JsonNode asText
public abstract String asText();
From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java
/** Get the Toolkit's path setting for a file access point. * @param ap the access point//from www . j a v a 2 s. com * @return the access point's file setting, if it has one, * or null otherwise. */ public static String getToolkitPath(final AccessPoint ap) { if (!"file".equals(ap.getType())) { // Not the right type. return null; } JsonNode dataJson = TaskUtils.jsonStringToTree(ap.getToolkitData()); JsonNode path = dataJson.get("path"); if (path == null) { return null; } return path.asText(); }
From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverter.java
/** * Convert this object, in the org.apache.kafka.connect.data format, into a JSON object, returning both the schema * and the converted object.// w w w . j a v a2s.c o m */ private static JsonNode convertToJson(Schema schema, Object logicalValue) { if (logicalValue == null) { if (schema == null) // Any schema is valid and we don't have a default, so treat this as an optional schema return null; if (schema.defaultValue() != null) return convertToJson(schema, schema.defaultValue()); if (schema.isOptional()) return JsonNodeFactory.instance.nullNode(); throw new DataException( "Conversion error: null value for field that is required and has no default value"); } Object value = logicalValue; try { final Schema.Type schemaType; if (schema == null) { schemaType = ConnectSchema.schemaType(value.getClass()); if (schemaType == null) throw new DataException( "Java class " + value.getClass() + " does not have corresponding schema type."); } else { schemaType = schema.type(); } switch (schemaType) { case INT8: return JsonNodeFactory.instance.numberNode((Byte) value); case INT16: return JsonNodeFactory.instance.numberNode((Short) value); case INT32: if (schema != null && Date.LOGICAL_NAME.equals(schema.name())) { return JsonNodeFactory.instance.textNode(ISO_DATE_FORMAT.format((java.util.Date) value)); } if (schema != null && Time.LOGICAL_NAME.equals(schema.name())) { return JsonNodeFactory.instance.textNode(TIME_FORMAT.format((java.util.Date) value)); } return JsonNodeFactory.instance.numberNode((Integer) value); case INT64: String schemaName = schema.name(); if (Timestamp.LOGICAL_NAME.equals(schemaName)) { return JsonNodeFactory.instance .numberNode(Timestamp.fromLogical(schema, (java.util.Date) value)); } return JsonNodeFactory.instance.numberNode((Long) value); case FLOAT32: return JsonNodeFactory.instance.numberNode((Float) value); case FLOAT64: return JsonNodeFactory.instance.numberNode((Double) value); case BOOLEAN: return JsonNodeFactory.instance.booleanNode((Boolean) value); case STRING: CharSequence charSeq = (CharSequence) value; return JsonNodeFactory.instance.textNode(charSeq.toString()); case BYTES: if (Decimal.LOGICAL_NAME.equals(schema.name())) { return JsonNodeFactory.instance.numberNode((BigDecimal) value); } byte[] valueArr = null; if (value instanceof byte[]) valueArr = (byte[]) value; else if (value instanceof ByteBuffer) valueArr = ((ByteBuffer) value).array(); if (valueArr == null) throw new DataException("Invalid type for bytes type: " + value.getClass()); return JsonNodeFactory.instance.binaryNode(valueArr); case ARRAY: { Collection collection = (Collection) value; ArrayNode list = JsonNodeFactory.instance.arrayNode(); for (Object elem : collection) { Schema valueSchema = schema == null ? null : schema.valueSchema(); JsonNode fieldValue = convertToJson(valueSchema, elem); list.add(fieldValue); } return list; } case MAP: { Map<?, ?> map = (Map<?, ?>) value; // If true, using string keys and JSON object; if false, using non-string keys and Array-encoding boolean objectMode; if (schema == null) { objectMode = true; for (Map.Entry<?, ?> entry : map.entrySet()) { if (!(entry.getKey() instanceof String)) { objectMode = false; break; } } } else { objectMode = schema.keySchema().type() == Schema.Type.STRING; } ObjectNode obj = null; ArrayNode list = null; if (objectMode) obj = JsonNodeFactory.instance.objectNode(); else list = JsonNodeFactory.instance.arrayNode(); for (Map.Entry<?, ?> entry : map.entrySet()) { Schema keySchema = schema == null ? null : schema.keySchema(); Schema valueSchema = schema == null ? null : schema.valueSchema(); JsonNode mapKey = convertToJson(keySchema, entry.getKey()); JsonNode mapValue = convertToJson(valueSchema, entry.getValue()); if (objectMode) obj.set(mapKey.asText(), mapValue); else list.add(JsonNodeFactory.instance.arrayNode().add(mapKey).add(mapValue)); } return objectMode ? obj : list; } case STRUCT: { Struct struct = (Struct) value; if (struct.schema() != schema) throw new DataException("Mismatching schema."); ObjectNode obj = JsonNodeFactory.instance.objectNode(); for (Field field : schema.fields()) { obj.set(field.name(), convertToJson(field.schema(), struct.get(field))); } return obj; } } throw new DataException("Couldn't convert " + value + " to JSON."); } catch (ClassCastException e) { throw new DataException("Invalid type for " + schema.type() + ": " + value.getClass()); } }
From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java
/** Get the portal's format setting for a file access point. * @param ap the access point/*w w w .j a v a 2 s .c o m*/ * @return the access point's format setting, if it has one, * or null otherwise. */ public static String getFormat(final AccessPoint ap) { if (!"file".equals(ap.getType())) { // Not the right type. return null; } JsonNode dataJson = TaskUtils.jsonStringToTree(ap.getPortalData()); JsonNode format = dataJson.get("format"); if (format == null) { return null; } return format.asText(); }
From source file:com.redhat.lightblue.query.UnsetExpression.java
/** * Parses an unset expression using the given json object *//*from ww w .j a v a2s. c o m*/ public static UnsetExpression fromJson(ObjectNode node) { if (node.size() == 1) { JsonNode val = node.get(UpdateOperator._unset.toString()); if (val != null) { List<Path> fields = new ArrayList<>(); if (val instanceof ArrayNode) { for (Iterator<JsonNode> itr = ((ArrayNode) val).elements(); itr.hasNext();) { fields.add(new Path(itr.next().asText())); } } else if (val.isValueNode()) { fields.add(new Path(val.asText())); } return new UnsetExpression(fields); } } throw Error.get(QueryConstants.ERR_INVALID_UNSET_EXPRESSION, node.toString()); }
From source file:de.thingweb.desc.ThingDescriptionParser.java
private static List<String> stringOrArray(JsonNode node) { List<String> array = new ArrayList<String>(); if (node.isTextual()) { array.add(node.asText()); } else if (node.isArray()) { for (JsonNode subnode : node) { array.add(subnode.asText()); }//from w ww. ja v a 2 s . co m } return array; }
From source file:com.baasbox.service.scripting.ScriptingService.java
private static ODocument createScript(ScriptsDao dao, JsonNode node) throws ScriptException { updateCacheVersion();/* w w w. j a v a 2s . co m*/ String lang = node.get(ScriptsDao.LANG).asText(); ScriptLanguage language = ScriptLanguage.forName(lang); String name = node.get(ScriptsDao.NAME).asText(); String code = node.get(ScriptsDao.CODE).asText(); JsonNode initialStorage = node.get(ScriptsDao.LOCAL_STORAGE); JsonNode library = node.get(ScriptsDao.LIB); boolean isLibrary = library == null ? false : library.asBoolean(); JsonNode activeNode = node.get(ScriptsDao.ACTIVE); boolean active = activeNode == null ? false : activeNode.asBoolean(); JsonNode encoded = node.get(ScriptsDao.ENCODED); ODocument doc; if (encoded != null && encoded.isTextual()) { String encodedValue = encoded.asText(); doc = dao.create(name, language.name, code, isLibrary, active, initialStorage, encodedValue); } else { doc = dao.create(name, language.name, code, isLibrary, active, initialStorage); } return doc; }
From source file:com.baasbox.service.scripting.ScriptingService.java
private static void append(List<String> list, JsonNode node) { if (node == null || node.isNull() || node.isMissingNode() || node.isObject()) return;/*from www . ja v a2 s.c o m*/ if (node.isValueNode()) list.add(node.asText()); if (node.isArray()) { node.forEach((n) -> { if (n != null && (!n.isNull()) && (!n.isMissingNode()) && n.isValueNode()) list.add(n.toString()); }); } }
From source file:com.pros.jsontransform.filter.ArrayFilterContains.java
public static boolean evaluate(final JsonNode filterNode, final JsonNode elementNode, final ObjectTransformer transformer) throws ObjectTransformerException { // contains only supports string type String fieldValue, likeValue; boolean result = false; JsonNode filterArguments = filterNode.get(ArrayFilter.$CONTAINS.name().toLowerCase()); if (elementNode.isObject()) { // compare objects fieldValue = transformer.transformValueNode(elementNode, filterArguments).asText(); } else {/* w w w . ja v a2 s .co m*/ // compare values fieldValue = elementNode.asText(); } likeValue = filterArguments.path(ArrayFilter.ARGUMENT_WHAT).asText(); if (fieldValue.contains(likeValue)) { result = true; } return result; }
From source file:com.ning.metrics.serialization.event.SmileEnvelopeEvent.java
@SuppressWarnings("deprecation") public static Granularity getGranularityFromJson(final JsonNode node) { final JsonNode granularityNode = node.get(SMILE_EVENT_GRANULARITY_TOKEN_NAME); if (granularityNode == null) { return Granularity.HOURLY; }//from w w w .j a v a 2 s . com try { return Granularity.valueOf(granularityNode.asText()); } catch (IllegalArgumentException e) { // hmmmh. Returning null seems dangerous; but that's what we had... return null; } }
From source file:es.bsc.amon.util.tree.TreeNodeFactory.java
public static TreeNode fromJson(JsonNode json) { TreeNode n = null;// www. j a va 2s .c o m if (json.isArray()) { n = new NodeArray(); Iterator<JsonNode> it = json.iterator(); while (it.hasNext()) { ((NodeArray) n).elements.add(fromJson(it.next())); } } else if (json.isNumber()) { n = new NumberValue(); ((NumberValue) n).value = json.numberValue(); } else if (json.isTextual()) { n = new StringValue(); ((StringValue) n).value = json.asText(); } else if (json.isObject()) { n = new ObjNode(); Iterator<Map.Entry<String, JsonNode>> it = json.fields(); while (it.hasNext()) { Map.Entry<String, JsonNode> e = it.next(); ((ObjNode) n).properties.put(e.getKey(), fromJson(e.getValue())); } } else throw new RuntimeException("You should not reach this"); return n; }