List of usage examples for com.fasterxml.jackson.databind JsonNode asText
public abstract String asText();
From source file:org.springframework.tuple.JsonNodeToTupleConverter.java
@Override public Tuple convert(JsonNode root) { TupleBuilder builder = TupleBuilder.tuple(); if (root.isValueNode()) { return builder.of("value", root.asText()); }/*from w w w .j a v a 2s . c om*/ try { for (Iterator<Entry<String, JsonNode>> it = root.fields(); it.hasNext();) { Entry<String, JsonNode> entry = it.next(); String name = entry.getKey(); JsonNode node = entry.getValue(); if (node.isObject()) { // tuple builder.addEntry(name, convert(node)); } else if (node.isArray()) { builder.addEntry(name, nodeToList(node)); } else if (node.isNull()) { builder.addEntry(name, null); } else if (node.isBoolean()) { builder.addEntry(name, node.booleanValue()); } else if (node.isNumber()) { builder.addEntry(name, node.numberValue()); } else { builder.addEntry(name, node.asText()); } } } catch (Exception e) { throw new RuntimeException(e); } return builder.build(); }
From source file:com.ikanow.aleph2.security.service.IkanowV1UserGroupRoleProvider.java
@Override public Tuple2<Set<String>, Set<String>> getRolesAndPermissions(String principalName) { Set<String> roleNames = new HashSet<String>(); Set<String> permissions = new HashSet<String>(); Optional<JsonNode> result; try {/* w w w .j a v a 2s. com*/ ObjectId objecId = new ObjectId(principalName); result = getPersonStore().getObjectBySpec(CrudUtils.anyOf().when("_id", objecId)).get(); if (result.isPresent()) { // community based roles JsonNode person = result.get(); JsonNode communities = person.get("communities"); if (communities != null && communities.isArray()) { if (communities.size() > 0) { roleNames.add(principalName); } for (final JsonNode community : communities) { JsonNode type = community.get("type"); if (type != null && "user".equalsIgnoreCase(type.asText())) { String communityId = community.get("_id").asText(); String communityName = community.get("name").asText(); String communityPermission = PermissionExtractor.createPermission( IkanowV1SecurityService.SECURITY_ASSET_COMMUNITY, Optional.of(ISecurityService.ACTION_WILDCARD), communityId); permissions.add(communityPermission); logger.debug("Permission (ShareIds) loaded for " + principalName + ",(" + communityName + "):" + communityPermission); } } } // communities } } catch (Exception e) { logger.error("Caught Exception", e); } logger.debug("Roles loaded for " + principalName + ":"); logger.debug(roleNames); return Tuples._2T(roleNames, permissions); }
From source file:io.gravitee.definition.jackson.datatype.api.deser.EndpointDeserializer.java
@Override public Endpoint deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode node = jp.getCodec().readTree(jp); Endpoint endpoint = new Endpoint(); endpoint.setTarget(node.get("target").asText()); JsonNode nameNode = node.get("name"); if (nameNode != null) { String name = nameNode.asText(); endpoint.setName(name);/* www. j a va2s .co m*/ } else { throw ctxt.mappingException("Endpoint name is required"); } JsonNode weightNode = node.get("weight"); if (weightNode != null) { int weight = weightNode.asInt(Endpoint.DEFAULT_WEIGHT); endpoint.setWeight(weight); } else { endpoint.setWeight(Endpoint.DEFAULT_WEIGHT); } JsonNode backupNode = node.get("backup"); if (backupNode != null) { boolean backup = backupNode.asBoolean(false); endpoint.setBackup(backup); } else { endpoint.setBackup(false); } JsonNode healthcheckNode = node.get("healthcheck"); if (healthcheckNode != null) { boolean healthcheck = healthcheckNode.asBoolean(true); endpoint.setHealthcheck(healthcheck); } else { endpoint.setHealthcheck(true); } JsonNode httpProxyNode = node.get("proxy"); if (httpProxyNode != null) { HttpProxy httpProxy = httpProxyNode.traverse(jp.getCodec()).readValueAs(HttpProxy.class); endpoint.setHttpProxy(httpProxy); } JsonNode httpClientOptionsNode = node.get("http"); if (httpClientOptionsNode != null) { HttpClientOptions httpClientOptions = httpClientOptionsNode.traverse(jp.getCodec()) .readValueAs(HttpClientOptions.class); endpoint.setHttpClientOptions(httpClientOptions); } else { endpoint.setHttpClientOptions(new HttpClientOptions()); } JsonNode httpClientSslOptionsNode = node.get("ssl"); if (httpClientSslOptionsNode != null) { HttpClientSslOptions httpClientSslOptions = httpClientSslOptionsNode.traverse(jp.getCodec()) .readValueAs(HttpClientSslOptions.class); endpoint.setHttpClientSslOptions(httpClientSslOptions); } return endpoint; }
From source file:com.strategicgains.hyperexpress.serialization.siren.jackson.SirenResourceDeserializer.java
/** * @param resource/* w ww . j a v a 2s . com*/ * @param field */ private void addAllLinks(SirenResource resource, Entry<String, JsonNode> field) { LinkBuilder lb = new LinkBuilder(); lb.rel(field.getKey()); Iterator<JsonNode> values = field.getValue().elements(); while (values.hasNext()) { JsonNode value = values.next(); Iterator<JsonNode> elements = value.elements(); while (elements.hasNext()) { JsonNode element = elements.next(); lb.set(element.asText(), element.textValue()); } resource.addLink(lb.build()); lb.clearAttributes(); } }
From source file:org.agorava.twitter.jackson.TweetDeserializer.java
@Override public Tweet deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode tree = jp.readValueAsTree(); long id = tree.get("id").asLong(); String text = tree.get("text").asText(); JsonNode fromUserNode = tree.get("user"); String fromScreenName = null; long fromId = 0; String fromImageUrl = null;/*w w w . j ava2 s .c o m*/ String dateFormat = TIMELINE_DATE_FORMAT; if (fromUserNode != null) { fromScreenName = fromUserNode.get("screen_name").asText(); fromId = fromUserNode.get("id").asLong(); fromImageUrl = fromUserNode.get("profile_image_url").asText(); } else { fromScreenName = tree.get("from_user").asText(); fromId = tree.get("from_user_id").asLong(); fromImageUrl = tree.get("profile_image_url").asText(); dateFormat = SEARCH_DATE_FORMAT; } Date createdAt = toDate(tree.get("created_at").asText(), new SimpleDateFormat(dateFormat, Locale.ENGLISH)); String source = tree.get("source").asText(); JsonNode toUserIdNode = tree.get("in_reply_to_user_id"); Long toUserId = toUserIdNode != null ? toUserIdNode.longValue() : null; JsonNode languageCodeNode = tree.get("iso_language_code"); String languageCode = languageCodeNode != null ? languageCodeNode.asText() : null; Tweet tweet = new Tweet(id, text, createdAt, fromScreenName, fromImageUrl, toUserId, fromId, languageCode, source); JsonNode inReplyToStatusIdNode = tree.get("in_reply_to_status_id"); Long inReplyToStatusId = inReplyToStatusIdNode != null && !inReplyToStatusIdNode.isNull() ? inReplyToStatusIdNode.longValue() : null; tweet.setInReplyToStatusId(inReplyToStatusId); JsonNode retweetCountNode = tree.get("retweet_count"); Integer retweetCount = retweetCountNode != null && !retweetCountNode.isNull() ? retweetCountNode.intValue() : null; tweet.setRetweetCount(retweetCount); JsonNode favoritedNode = tree.get("favorited"); boolean favorited = favoritedNode != null && !favoritedNode.isNull() ? favoritedNode.booleanValue() : false; tweet.setFavorited(favorited); jp.skipChildren(); return tweet; }
From source file:com.redhat.lightblue.metadata.types.DateTypeTest.java
@Test public void testToJson() { DateFormat dateFormat = new SimpleDateFormat(DateType.DATE_FORMAT_STR); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); String date = dateFormat.format(new Date()); JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true); JsonNode jsonNode = dateType.toJson(jsonNodeFactory, date); assertTrue(jsonNode.asText().equals(date)); }
From source file:org.springframework.social.facebook.api.impl.json.PictureDeserializer.java
@Override public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = jp.readValueAs(JsonNode.class); if (node.isObject() && node.has("data")) { return node.get("data").get("url").asText(); } else if (node.isTextual()) { return node.asText(); }//from www. ja va 2 s .com return null; }
From source file:com.reprezen.swagedit.schema.ObjectTypeDefinition.java
protected void initRequired() { JsonNode required = content.get("required"); if (required != null && required.isArray()) { for (JsonNode value : required) { requiredProperties.add(value.asText()); }/* ww w . j a v a2 s. c o m*/ } }
From source file:com.strategicgains.hyperexpress.serialization.jackson.HalResourceDeserializer.java
/** * @param resource/* w ww. j ava 2 s.c om*/ * @param field */ private void addAllLinks(HalResource resource, Entry<String, JsonNode> field) { LinkBuilder lb = new LinkBuilder(); lb.rel(field.getKey()); Iterator<JsonNode> values = field.getValue().elements(); while (values.hasNext()) { JsonNode value = values.next(); Iterator<JsonNode> elements = value.elements(); while (elements.hasNext()) { JsonNode element = elements.next(); lb.set(element.asText(), element.textValue()); } resource.addLink(lb.build()); lb.clearAttributes(); } }
From source file:com.redhat.lightblue.metadata.types.DateType.java
@Override public Object fromJson(JsonNode node) { if (node.isValueNode()) { try {/* ww w. j av a 2 s .c o m*/ return getDateFormat().parse(node.asText()); } catch (ParseException e) { throw Error.get(NAME, MetadataConstants.ERR_INCOMPATIBLE_VALUE, node.toString()); } } else { throw Error.get(NAME, MetadataConstants.ERR_INCOMPATIBLE_VALUE, node.toString()); } }