List of usage examples for com.fasterxml.jackson.databind JsonNode asText
public abstract String asText();
From source file:io.gravitee.definition.jackson.datatype.services.healthcheck.deser.RequestDeserializer.java
@Override public Request deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode node = jp.getCodec().readTree(jp); Request request = new Request(); JsonNode uriNode = node.get("uri"); if (uriNode != null) { request.setUri(uriNode.asText()); } else {/* w w w.j a v a2 s . co m*/ throw ctxt.mappingException("[healthcheck] URI is required"); } final JsonNode methodNode = node.get("method"); if (methodNode != null) { request.setMethod(HttpMethod.valueOf(methodNode.asText().toUpperCase())); } else { throw ctxt.mappingException("[healthcheck] Method is required"); } JsonNode bodyNode = node.get("body"); if (bodyNode != null) { request.setBody(bodyNode.asText()); } JsonNode headersNode = node.get("headers"); if (headersNode != null) { List<HttpHeader> headers = new ArrayList<>(); headersNode.elements().forEachRemaining(headerNode -> { HttpHeader header = new HttpHeader(); header.setName(headerNode.findValue("name").asText()); header.setValue(headerNode.findValue("value").asText()); headers.add(header); }); request.setHeaders(headers); } return request; }
From source file:org.ocsoft.olivia.models.contents.config.JsonObject.java
public String getAsString(String key) { JsonNode value = root.get(key); return value.asText(); }
From source file:lumbermill.internal.influxdb.InfluxDBClient.java
/** * Creates Points based on the event and config *///from ww w . j a va 2s . com private static Observable<Point> buildPoint(MapWrap config, StringTemplate measurementTemplate, JsonEvent jsonEvent) { final MapWrap fieldsConfig = MapWrap.of(config.getObject("fields")); final List<String> excludeTags = config.getObject("excludeTags", DEFAULT_EXCLUDED_TAGS); // One field is required, otherwise the point will not be created boolean addedAtLeastOneField = false; Optional<String> measurementOptional = measurementTemplate.format(jsonEvent); if (!measurementOptional.isPresent()) { LOGGER.debug("Failed to extract measurement using {}, not points will be created", measurementTemplate.original()); return Observable.empty(); } Point.Builder pointBuilder = Point.measurement(measurementOptional.get()); for (Object entry1 : fieldsConfig.toMap().entrySet()) { Map.Entry<String, String> entry = (Map.Entry) entry1; StringTemplate fieldName = StringTemplate.compile(entry.getKey()); String valueField = entry.getValue(); JsonNode node = jsonEvent.unsafe().get(valueField); if (node == null) { LOGGER.debug("Failed to extract any field for {}", valueField); continue; } Optional<String> formattedFieldNameOptional = fieldName.format(jsonEvent); if (!formattedFieldNameOptional.isPresent()) { LOGGER.debug("Failed to extract any field for {}", fieldName.original()); continue; } addedAtLeastOneField = true; if (node.isNumber()) { pointBuilder.addField(formattedFieldNameOptional.get(), node.asDouble()); } else if (node.isBoolean()) { pointBuilder.addField(formattedFieldNameOptional.get(), node.asBoolean()); } else { pointBuilder.addField(formattedFieldNameOptional.get(), node.asText()); } } Iterator<String> stringIterator = jsonEvent.unsafe().fieldNames(); while (stringIterator.hasNext()) { String next = stringIterator.next(); if (!excludeTags.contains(next)) { pointBuilder.tag(next, jsonEvent.valueAsString(next)); } } Optional<String> timeField = config.exists("time") ? Optional.of(config.asString("time")) : Optional.empty(); TimeUnit precision = config.getObject("precision", TimeUnit.MILLISECONDS); // Override @timestamp with a ISO_8601 String or a numerical value if (timeField.isPresent() && jsonEvent.has(config.asString("time"))) { if (jsonEvent.unsafe().get(timeField.get()).isTextual()) { pointBuilder.time(ZonedDateTime .parse(jsonEvent.valueAsString("@timestamp"), DateTimeFormatter.ISO_OFFSET_DATE_TIME) .toInstant().toEpochMilli(), precision); } else { pointBuilder.time(jsonEvent.asLong(timeField.get()), precision); } } else { // If not overriden, check if timestamp exists and use that if (jsonEvent.has("@timestamp")) { pointBuilder.time(ZonedDateTime .parse(jsonEvent.valueAsString("@timestamp"), DateTimeFormatter.ISO_OFFSET_DATE_TIME) .toInstant().toEpochMilli(), precision); } } if (!addedAtLeastOneField) { LOGGER.debug("Could not create a point since no fields where added"); return Observable.empty(); } Point point = pointBuilder.build(); if (LOGGER.isTraceEnabled()) { LOGGER.trace("Point to be stored {}", point.toString()); } return Observable.just(point); }
From source file:com.orasi.sandbox.TestXMLDataProvider.java
@Test(dataProvider = "xmlDataDiningNode") public void testXMLDiningNode(JsonNode node) { JsonNode nlist = node.path("diningList"); Iterator<String> i1 = new IteratorMap<JsonNode, String>(nlist.iterator()) { @Override// ww w . ja v a2 s .c om public String apply(JsonNode o) { return o.asText(); } }; List<String> l1 = Lists.newArrayList(i1); List<String> l2 = Lists.newArrayList(DINING_LIST); Assert.assertFalse(Collections.disjoint(l1, l2)); Assert.assertEquals(node.path("diningInfo").path("partySize").asInt(), 1); }
From source file:com.turn.shapeshifter.transformers.DateTimeTransformer.java
/** * @param node a date formatted using ISO 8601 *//*from w ww .ja v a2 s . co m*/ @Override public Object parse(JsonNode node) { Preconditions.checkArgument(JsonToken.VALUE_STRING.equals(node.asToken())); String nodeValue = node.asText(); return new Long(ISO_8601.parseMillis(nodeValue)); }
From source file:com.redhat.lightblue.crud.validator.EnumCheckerTest.java
/** * If the name on the Constraint is null, then an error should be created. *//*from ww w .j av a 2 s . c om*/ @Test public void testCheckConstraint_NullName() { ConstraintValidator validator = mock(ConstraintValidator.class); EnumConstraint constraint = new EnumConstraint(); constraint.setName(null); JsonNode fieldValue = mock(JsonNode.class); when(fieldValue.asText()).thenReturn("String value"); new EnumChecker().checkConstraint(validator, null, null, constraint, null, null, fieldValue); verify(validator, times(1)).addDocError(any(Error.class)); }
From source file:com.googlecode.jsonschema2pojo.rules.TitleRule.java
/** * Applies this schema rule to take the required code generation steps. * <p>//from www . jav a 2s .c o m * When a title node is found and applied with this rule, the value of the * title is added as a JavaDoc comment. This rule is typically applied to * the generated field, generated getter and generated setter for the * property. * <p> * Note that the title is always inserted at the top of the JavaDoc comment. * * @param nodeName * the name of the property to which this title applies * @param node * the "title" schema node * @param generatableType * comment-able code generation construct, usually a field or * method, which should have this title applied * @return the JavaDoc comment created to contain the title */ @Override public JDocComment apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) { JDocComment javadoc = generatableType.javadoc(); javadoc.add(0, node.asText() + "\n<p>\n"); return javadoc; }
From source file:org.apache.usergrid.activityfeed.ActivityEntity.java
@Nullable @JsonIgnore/*from ww w .j a v a 2s .co m*/ public String getDisplayName() { if (actor != null) { JsonNode displayName = actor.get("displayName"); if (displayName != null) { return displayName.asText(); } } return null; }
From source file:com.redhat.lightblue.crud.validator.EnumCheckerTest.java
/** * If the fieldValue's text value is not in the returned Enum's values, then an error should * be created./*from w w w . j av a 2s .c o m*/ */ @Test public void testCheckConstraint_WithEnum_ButFieldValueNotInSet() { final String name = "Fake Name"; Enum e = new Enum("Fake Enum"); ConstraintValidator validator = mock(ConstraintValidator.class); mockEnum(validator, e); EnumConstraint constraint = new EnumConstraint(); constraint.setName(name); JsonNode fieldValue = mock(JsonNode.class); when(fieldValue.asText()).thenReturn(name); new EnumChecker().checkConstraint(validator, null, null, constraint, null, null, fieldValue); verify(validator, times(1)).addDocError(any(Error.class)); }
From source file:com.redhat.lightblue.crud.validator.EnumCheckerTest.java
/** * If the fieldValue's text value is in the returned Enum's values, then an error should * not be created./*from w ww.ja va 2 s. com*/ */ @Test public void testCheckConstraint_WithEnum_FieldValueIsInSet() { final String name = "Fake Name"; Enum e = new Enum(name); e.setValues(new HashSet<EnumValue>(Arrays.asList(new EnumValue(name, null)))); ConstraintValidator validator = mock(ConstraintValidator.class); mockEnum(validator, e); EnumConstraint constraint = new EnumConstraint(); constraint.setName(name); JsonNode fieldValue = mock(JsonNode.class); when(fieldValue.asText()).thenReturn(name); new EnumChecker().checkConstraint(validator, null, null, constraint, null, null, fieldValue); verify(validator, never()).addDocError(any(Error.class)); }