List of usage examples for com.fasterxml.jackson.databind JsonNode textValue
public String textValue()
From source file:org.eel.kitchen.jsonschema.syntax.TypeKeywordSyntaxChecker.java
private static void validateOne(final Message.Builder msg, final List<Message> messages, final JsonNode value) { // Cannot happen in the event of single property validation (will // always be a string) if (value.isObject()) return;/*w w w . j ava 2 s . c o m*/ // See above if (!value.isTextual()) { msg.addInfo("found", NodeType.getNodeType(value)).setMessage("array element has incorrect type") .addInfo("expected", VALID_TYPE_ARRAY_ELEMENTS); messages.add(msg.build()); return; } // Now we can actually check that the string is a valid primitive type final String s = value.textValue(); if (ANY.equals(s)) return; if (NodeType.fromName(s) != null) return; msg.addInfo("possible-values", EnumSet.allOf(NodeType.class)).addInfo("found", s) .setMessage("unknown simple type"); messages.add(msg.build()); }
From source file:com.attribyte.essem.model.StoredGraph.java
/** * Creates a stored graph from JSON./*from w w w . j a v a 2 s . c o m*/ * @param obj The JSON object. * @return The stored graph. * @throws IOException on parse error. */ public static final StoredGraph fromJSON(final JsonNode obj) throws IOException { String uid = getStringField(obj, "uid"); String index = getStringField(obj, "index"); String application = getStringField(obj, "application"); String host = getStringField(obj, "host"); String instance = getStringField(obj, "instance"); String name = getStringField(obj, "name"); String field = getStringField(obj, "field"); MetricKey key = new MetricKey(name, application, host, instance, field); String downsampleFn = getStringField(obj, "downsampleFn"); String rateUnit = getStringField(obj, "rateUnit"); String range = getStringField(obj, "range"); long startTimestamp = getLongField(obj, "startTimestamp", 0); long endTimestamp = getLongField(obj, "endTimestamp", 0); String title = getStringField(obj, "title"); String description = getStringField(obj, "description"); String xLabel = getStringField(obj, "xLabel"); String yLabel = getStringField(obj, "yLabel"); Set<String> tags = null; if (obj.has("tag")) { tags = Sets.newHashSetWithExpectedSize(8); JsonNode tagNode = obj.get("tag"); if (tagNode.isArray()) { for (JsonNode arrNode : tagNode) { tags.add(arrNode.textValue()); } } else if (tagNode.isTextual()) { tags.add(tagNode.textValue()); } } String createdString = getStringField(obj, "created"); Date createTime = Strings.isNullOrEmpty(createdString) ? new Date() : new Date(DateTime.fromStandardFormat(createdString)); return new StoredGraph(index, uid, key, range, startTimestamp, endTimestamp, downsampleFn, rateUnit, title, description, xLabel, yLabel, tags, createTime); }
From source file:org.activiti.app.service.editor.ServiceParameters.java
/** * Creates a new {@link ServiceParameters} instance based on all properties in the given * object node. Only numbers, text and boolean values are added, nested object structures * are ignored.// w ww . j a v a 2 s . c o m */ public static ServiceParameters fromObjectNode(ObjectNode node) { ServiceParameters parameters = new ServiceParameters(); Iterator<String> ir = node.fieldNames(); String name = null; JsonNode value = null; while (ir.hasNext()) { name = ir.next(); value = node.get(name); // Depending on the type, extract the raw value object if (value != null) { if (value.isNumber()) { parameters.addParameter(name, value.numberValue()); } else if (value.isBoolean()) { parameters.addParameter(name, value.booleanValue()); } else if (value.isTextual()) { parameters.addParameter(name, value.textValue()); } } } return parameters; }
From source file:org.level28.android.moca.json.ScheduleDeserializer.java
private static Session parseSession(final JsonNode objectRoot) throws JsonDeserializerException { // Basic sanity checks if (objectRoot == null || objectRoot.isNull()) { throw new JsonDeserializerException("null objectRoot"); }/*w w w .j a va2 s . c o m*/ if (!objectRoot.isObject()) { throw new JsonDeserializerException("objectRoot is not a JSON object"); } JsonNode node; Session result = new Session(); try { // Session id (required) node = objectRoot.path("id"); if (node.isMissingNode() || !node.isTextual()) { throw new JsonDeserializerException("'id' is missing or invalid"); } result.setId(node.textValue()); // Session title (required) node = objectRoot.path("title"); if (node.isMissingNode() || !node.isTextual()) { throw new JsonDeserializerException("'title' is missing or invalid"); } result.setTitle(node.textValue()); // Session day (required) node = objectRoot.path("day"); if (node.isMissingNode() || !node.isInt()) { throw new JsonDeserializerException("'day' is missing or invalid"); } result.setDay(node.asInt()); // Session start time (required) node = objectRoot.path("start"); if (node.isMissingNode() || !node.isTextual()) { throw new JsonDeserializerException("'start' is missing or invalid"); } result.setStartTime(parseTime(node.textValue())); // Session end time (required) node = objectRoot.path("end"); if (node.isMissingNode() || !node.isTextual()) { throw new JsonDeserializerException("'end' is missing or invalid"); } result.setEndTime(parseTime(node.textValue())); // Session hosts (required) node = objectRoot.path("hosts"); if (node.isMissingNode() || !node.isArray()) { throw new JsonDeserializerException("'hosts' is missing or invalid"); } final ArrayList<String> hosts = new ArrayList<String>(node.size()); for (JsonNode hostsSubNode : node) { if (!hostsSubNode.isTextual() || "".equals(hostsSubNode.textValue())) { throw new JsonDeserializerException("'hosts' children is not valid"); } hosts.add(hostsSubNode.textValue()); } result.setHosts(TextUtils.join(", ", hosts)); // Session language (required) node = objectRoot.path("lang"); if (node.isMissingNode() || !node.isTextual()) { throw new JsonDeserializerException("'lang' is missing or invalid"); } result.setLang(node.textValue()); // Session abstract (optional) node = objectRoot.path("abstract"); if (!node.isMissingNode()) { result.setSessionAbstract(node.textValue()); } return result; } catch (IllegalArgumentException e) { throw new JsonDeserializerException("Invalid session entry", e); } catch (ParseException e) { throw new JsonDeserializerException("Invalid session entry", e); } }
From source file:org.jetbrains.webdemo.examples.ExamplesLoader.java
private static ExamplesFolder loadFolder(String path, String url, List<ObjectNode> parentCommonFiles, ExamplesFolder parentFolder) {/*from w w w. j av a2s . c om*/ File manifestFile = new File(path + File.separator + "manifest.json"); try (BufferedInputStream reader = new BufferedInputStream(new FileInputStream(manifestFile))) { ObjectNode manifest = (ObjectNode) JsonUtils.getObjectMapper().readTree(reader); String name = new File(path).getName(); List<ObjectNode> commonFiles = new ArrayList<>(); commonFiles.addAll(parentCommonFiles); boolean taskFolder = manifest.has("taskFolder") ? manifest.get("taskFolder").asBoolean() : false; List<LevelInfo> levels = null; if (manifest.has("levels")) { levels = new ArrayList<>(); for (JsonNode level : manifest.get("levels")) { levels.add(new LevelInfo(level.get("projectsNeeded").asInt(), level.get("color").asText())); } } if (parentFolder != null && parentFolder.isTaskFolder()) taskFolder = true; ExamplesFolder folder = new ExamplesFolder(name, url, taskFolder, levels); if (manifest.has("files")) { for (JsonNode node : manifest.get("files")) { ObjectNode fileManifest = (ObjectNode) node; fileManifest.put("path", path + File.separator + fileManifest.get("filename").asText()); commonFiles.add(fileManifest); } } if (manifest.has("folders")) { for (JsonNode node : manifest.get("folders")) { String folderName = node.textValue(); folder.addChildFolder(loadFolder(path + File.separator + folderName, url + folderName + "/", commonFiles, folder)); } } if (manifest.has("examples")) { for (JsonNode node : manifest.get("examples")) { String projectName = node.textValue(); String projectPath = path + File.separator + projectName; folder.addExample(loadExample(projectPath, url, ApplicationSettings.LOAD_TEST_VERSION_OF_EXAMPLES, commonFiles)); } } return folder; } catch (IOException e) { System.err.println("Can't load folder: " + e.toString()); return null; } }
From source file:com.redhat.lightblue.query.Value.java
/** * Creates a value from a json node//from w w w. ja v a2 s .c o m * * If the node is decimal, double, or float, create s a BigDecimal value. If * the node is BigInteger, creates a BigIngeter value. If the node is a long * or int, creates a long or int value. If the node is a boolean, creates a * boolean value. Otherwise, creates a string value. */ public static Value fromJson(JsonNode node) { if (node.isValueNode()) { Object v = null; if (node.isNumber()) { if (node.isBigDecimal() || node.isDouble() || node.isFloat()) { v = node.decimalValue(); } else if (node.isBigInteger()) { v = node.bigIntegerValue(); } else if (node.isLong()) { v = node.longValue(); } else { v = node.intValue(); } } else if (node.isBoolean()) { v = node.booleanValue(); } else { v = node.textValue(); } return new Value(v); } else { throw Error.get(QueryConstants.ERR_INVALID_VALUE, node.toString()); } }
From source file:com.palominolabs.crm.sf.rest.RestConnectionImpl.java
@Nonnull private static RestSObject getSObject(JsonNode rawNode) throws IOException { ObjectNode jsonNode = asObjectNode(rawNode); ObjectNode attributes = getObjectNode(jsonNode, ATTRIBUTES_KEY); String type = getString(attributes, "type"); JsonNode idNode = jsonNode.get(ID_KEY); RestSObjectImpl sObject;//from w w w . j a va2 s.com if (isNull(idNode)) { sObject = RestSObjectImpl.getNew(type); } else { if (!idNode.isTextual()) { throw new ResponseParseException("Id node <" + idNode + "> wasn't textual"); } sObject = RestSObjectImpl.getNewWithId(type, new Id(idNode.textValue())); } jsonNode.remove(ID_KEY); jsonNode.remove(ATTRIBUTES_KEY); Iterator<String> fieldNames = jsonNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode fieldValueNode = jsonNode.get(fieldName); if (fieldValueNode.isNull()) { // null node is a value node so handle it first sObject.setField(fieldName, null); continue; } else if (fieldValueNode.isValueNode()) { sObject.setField(fieldName, fieldValueNode.asText()); continue; } else if (fieldValueNode.isObject()) { // it could either be a subquery or a sub object at this point. if (fieldValueNode.path("attributes").isObject()) { sObject.setRelationshipSubObject(fieldName, getSObject(fieldValueNode)); } else if (fieldValueNode.path("records").isArray()) { sObject.setRelationshipQueryResult(fieldName, getQueryResult(fieldValueNode)); } else { throw new ResponseParseException("Could not understand field value node: " + fieldValueNode); } continue; } throw new ResponseParseException("Unknown node type <" + fieldValueNode + ">"); } return sObject; }
From source file:org.openmhealth.shim.jawbone.mapper.JawboneDataPointMapper.java
/** * Translates a time zone descriptor from one of various representations (Olson, seconds offset, GMT offset) into a * {@link ZoneId}.//from ww w. j a v a2 s.c om * * @param timeZoneValueNode the value associated with a timezone property */ static ZoneId parseZone(JsonNode timeZoneValueNode) { // default to UTC if timezone is not present if (timeZoneValueNode.isNull()) { return ZoneOffset.UTC; } // "-25200" if (timeZoneValueNode.asInt() != 0) { ZoneOffset zoneOffset = ZoneOffset.ofTotalSeconds(timeZoneValueNode.asInt()); // TODO confirm if this is even necessary, since ZoneOffset is a ZoneId return ZoneId.ofOffset("GMT", zoneOffset); } // e.g., "GMT-0700" or "America/Los_Angeles" if (timeZoneValueNode.isTextual()) { return ZoneId.of(timeZoneValueNode.textValue()); } throw new IllegalArgumentException(format("The time zone node '%s' can't be parsed.", timeZoneValueNode)); }
From source file:com.unboundid.scim2.common.utils.JsonUtils.java
/** * Try to parse out a date from a JSON text node. * * @param node The JSON node to parse./*from w w w. j av a 2s . c o m*/ * * @return A parsed date instance or {@code null} if the text is not an * ISO8601 formatted date and time string. */ private static Date dateValue(final JsonNode node) { String text = node.textValue().trim(); if (text.length() >= 19 && Character.isDigit(text.charAt(0)) && Character.isDigit(text.charAt(1)) && Character.isDigit(text.charAt(2)) && Character.isDigit(text.charAt(3)) && text.charAt(4) == '-') { try { return ISO8601Utils.parse(text, new ParsePosition(0)); } catch (ParseException e) { // This is not a date after all. } } return null; }
From source file:com.heliosapm.tsdblite.json.JSON.java
public static Map<String, String> from(final JsonNode node) { if (node == null) return EMPTY_MAP; final int size = node.size(); if (size == 0) return EMPTY_MAP; final Map<String, String> map = new HashMap<String, String>(node.size()); for (Iterator<String> iter = node.fieldNames(); iter.hasNext();) { final String key = iter.next(); final JsonNode v = node.get(key); if (v.isTextual()) { map.put(key, v.textValue()); }/* www.j av a2 s.c o m*/ } return map; }