com.fasterxml.jackson.databind.JsonNode.asText()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(273)

本文整理了Java中com.fasterxml.jackson.databind.JsonNode.asText()方法的一些代码示例,展示了JsonNode.asText()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonNode.asText()方法的具体详情如下:
包路径:com.fasterxml.jackson.databind.JsonNode
类名称:JsonNode
方法名:asText

JsonNode.asText介绍

[英]Method that will return a valid String representation of the container value, if the node is a value node (method #isValueNode returns true), otherwise empty String.
[中]方法,如果节点是值节点(方法#isValueNode返回true),则返回容器值的有效字符串表示形式,否则返回空字符串。

代码示例

代码示例来源:origin: swagger-api/swagger-core

private String getFieldText(String fieldName, JsonNode node) {
    JsonNode inNode = node.get(fieldName);
    if (inNode != null) {
      return inNode.asText();
    }
    return null;
  }
}

代码示例来源:origin: apache/kafka

private static Object convert(JsonNode value) {
  if (value.isArray()) {
    List<String> retvalList = new ArrayList<>();
    for (JsonNode arrayElement : value)
      retvalList.add(arrayElement.asText());
    return retvalList;
  }
  return value.getNodeType() == JsonNodeType.NUMBER ? value.numberValue() : value.asText();
}

代码示例来源:origin: aws/aws-sdk-java

/**
   * Get a string field from the JSON.
   *
   * @param fieldName Name of field to get.
   * @return String value or null if not present.
   */
  private String getString(String fieldName) {
    return source.has(fieldName) ? source.get(fieldName).asText() : null;
  }
}

代码示例来源:origin: Alluxio/alluxio

/**
  * Deserialize an AccessControlList object.
  * @param jsonParser the json parser
  * @param deserializationContext deserializationcontext
  * @return
  * @throws IOException
  * @throws JsonProcessingException
  */
 @Override
 public AccessControlList deserialize(JsonParser jsonParser,
   DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
  JsonNode node = jsonParser.getCodec().readTree(jsonParser);
  String owner = node.get(OWNER_FIELD).asText();
  String owningGroup = node.get(OWNING_GROUP_FIELD).asText();
  List<String> stringEntries = new ArrayList<>();
  Iterator<JsonNode> nodeIterator = node.get(STRING_ENTRY_FIELD).elements();
  while (nodeIterator.hasNext()) {
   stringEntries.add(nodeIterator.next().asText());
  }
  return AccessControlList.fromStringEntries(owner, owningGroup, stringEntries);
 }
}

代码示例来源:origin: Activiti/Activiti

protected List<String> getValueAsList(String name,
                   JsonNode objectNode) {
  List<String> resultList = new ArrayList<String>();
  JsonNode valuesNode = objectNode.get(name);
  if (valuesNode != null) {
    for (JsonNode valueNode : valuesNode) {
      if (valueNode.get("value") != null && !valueNode.get("value").isNull()) {
        resultList.add(valueNode.get("value").asText());
      }
    }
  }
  return resultList;
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Transforms the {@link JsonNode} into a map to integrate with the {@link SignatureChecker} utility.
 *
 * @param messageJson JSON of message.
 * @return Transformed map.
 */
private Map<String, String> toMap(JsonNode messageJson) {
  Map<String, String> fields = new HashMap<String, String>(messageJson.size());
  Iterator<Map.Entry<String, JsonNode>> jsonFields = messageJson.fields();
  while (jsonFields.hasNext()) {
    Map.Entry<String, JsonNode> next = jsonFields.next();
    fields.put(next.getKey(), next.getValue().asText());
  }
  return fields;
}

代码示例来源:origin: Activiti/Activiti

public static List<String> getPropertyValueAsList(String name, JsonNode objectNode) {
 List<String> resultList = new ArrayList<String>();
 JsonNode propertyNode = getProperty(name, objectNode);
 if (propertyNode != null && !"null".equalsIgnoreCase(propertyNode.asText())) {
  String propertyValue = propertyNode.asText();
  String[] valueList = propertyValue.split(",");
  for (String value : valueList) {
   resultList.add(value.trim());
  }
 }
 return resultList;
}

代码示例来源:origin: Activiti/Activiti

protected List<String> getActiveValueList(List<String> originalValues, String propertyName, ObjectNode taskElementProperties) {
  List<String> activeValues = originalValues;
  if (taskElementProperties != null) {
   JsonNode overrideValuesNode = taskElementProperties.get(propertyName);
   if (overrideValuesNode != null) {
    if (overrideValuesNode.isNull() || !overrideValuesNode.isArray()  || overrideValuesNode.size() == 0) {
     activeValues = null;
    } else {
     activeValues = new ArrayList<String>();
     for (JsonNode valueNode : overrideValuesNode) {
      activeValues.add(valueNode.asText());
     }
    }
   }
  }
  return activeValues;
 }
}

代码示例来源:origin: Activiti/Activiti

Map<String, JsonNode> sourceRefMap) {
if (objectNode.get(EDITOR_CHILD_SHAPES) != null) {
  for (JsonNode jsonChildNode : objectNode.get(EDITOR_CHILD_SHAPES)) {
      JsonNode targetNode = childNode.get("target");
      if (targetNode != null && !targetNode.isNull()) {
        String targetRefId = targetNode.get(EDITOR_SHAPE_ID).asText();
        List<JsonNode> sourceAndTargetList = new ArrayList<JsonNode>();
        sourceAndTargetList.add(sourceRefMap.get(childNode.get(EDITOR_SHAPE_ID).asText()));
        sourceAndTargetList.add(shapeMap.get(targetRefId));
        sourceAndTargetMap.put(childEdgeId,
                    sourceAndTargetList);

代码示例来源:origin: Activiti/Activiti

protected FlowElement convertJsonToElement(JsonNode elementNode, JsonNode modelNode, Map<String, JsonNode> shapeMap) {
 ServiceTask serviceTask = new ServiceTask();
 serviceTask.setType(ServiceTask.DMN_TASK);
 
 JsonNode decisionTableReferenceNode = getProperty(PROPERTY_DECISIONTABLE_REFERENCE, elementNode);
 if (decisionTableReferenceNode != null && decisionTableReferenceNode.has("id") && !(decisionTableReferenceNode.get("id").isNull())) {
  String decisionTableId = decisionTableReferenceNode.get("id").asText();
  if (decisionTableMap != null) {
   String decisionTableKey = decisionTableMap.get(decisionTableId);
   FieldExtension decisionTableKeyField = new FieldExtension();
   decisionTableKeyField.setFieldName(PROPERTY_DECISIONTABLE_REFERENCE_KEY);
   decisionTableKeyField.setStringValue(decisionTableKey);
   serviceTask.getFieldExtensions().add(decisionTableKeyField);
  }
 }
 return serviceTask;
}

代码示例来源:origin: swagger-api/swagger-core

@Override
  public Callback deserialize(JsonParser jp, DeserializationContext ctxt)
      throws IOException, JsonProcessingException {
    Callback result = new Callback();
    JsonNode node = jp.getCodec().readTree(jp);
    ObjectNode objectNode = (ObjectNode)node;
    Map<String, Object> extensions = new LinkedHashMap<>();
    for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) {
      String childName = it.next();
      JsonNode child = objectNode.get(childName);
      // if name start with `x-` consider it an extesion
      if (childName.startsWith("x-")) {
        extensions.put(childName, Json.mapper().convertValue(child, Object.class));
      } else if (childName.equals("$ref")) {
        result.$ref(child.asText());
      } else {
        result.put(childName, Json.mapper().convertValue(child, PathItem.class));
      }
    }
    if (!extensions.isEmpty()) {
      result.setExtensions(extensions);
    }
    return result;
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void reportSpan() throws Exception {
  reporter.report(mock(SpanContextInformation.class), getSpan(100));
  elasticsearchClient.waitForCompletion();
  refresh();
  final JsonNode hits = elasticsearchClient.getJson("/stagemonitor-spans*/_search").get("hits");
  assertThat(hits.get("total").intValue()).as(hits.toString()).isEqualTo(1);
  final JsonNode spanJson = hits.get("hits").elements().next().get("_source");
  assertThat(spanJson.get("type").asText()).as(spanJson.toString()).isEqualTo("jdbc");
  assertThat(spanJson.get("method").asText()).as(spanJson.toString()).isEqualTo("SELECT");
  assertThat(spanJson.get("db.statement")).as(spanJson.toString()).isNotNull();
  assertThat(spanJson.get("db.statement").asText()).as(spanJson.toString()).isEqualTo("SELECT * from STAGEMONITOR where 1 < 2");
  assertThat(spanJson.get("duration_ms").asInt()).as(spanJson.toString()).isEqualTo(100);
  assertThat(spanJson.get("name").asText()).as(spanJson.toString()).isEqualTo("ElasticsearchExternalSpanReporterIntegrationTest#test");
}

代码示例来源:origin: Activiti/Activiti

protected static String getActiveValue(String originalValue, String propertyName, ObjectNode elementProperties) {
 String activeValue = originalValue;
 if (elementProperties != null) {
  JsonNode overrideValueNode = elementProperties.get(propertyName);
  if (overrideValueNode != null) {
   if (overrideValueNode.isNull()) {
    activeValue = null;
   } else {
    activeValue = overrideValueNode.asText();
   }
  }
 }
 return activeValue;
}

代码示例来源:origin: stagemonitor/stagemonitor

private void addMinIntervalToPanels(ObjectNode dashboard, String interval) {
  for (JsonNode row : dashboard.get("rows")) {
    for (JsonNode panel : row.get("panels")) {
      if (panel.has("datasource") && panel.get("datasource").asText().equals(ES_STAGEMONITOR_DS_NAME)) {
        ((ObjectNode) panel).put("interval", "$Interval");
      }
    }
  }
  for (JsonNode template : dashboard.get("templating").get("list")) {
    if (template.has("name") && "Interval".equals(template.get("name").asText())) {
      ((ObjectNode) template).put("auto_min", interval);
    }
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testParameters() {
  final SpanWrapper span = createTestSpan(1, s -> SpanUtils.setParameters(s, Collections.singletonMap("foo", "bar")));
  final ObjectNode jsonSpan = JsonUtils.toObjectNode(span);
  assertThat(jsonSpan.get("parameters")).isNotNull();
  assertThat(jsonSpan.get("parameters").get(0)).isNotNull();
  assertThat(jsonSpan.get("parameters").get(0).get("key").asText()).isEqualTo("foo");
  assertThat(jsonSpan.get("parameters").get(0).get("value").asText()).isEqualTo("bar");
}

代码示例来源:origin: apache/incubator-pinot

public SegmentZKMetadataCustomMapModifier(@Nonnull String jsonString)
  throws IOException {
 JsonNode jsonNode = JsonUtils.stringToJsonNode(jsonString);
 _modifyMode = ModifyMode.valueOf(jsonNode.get(MAP_MODIFY_MODE_KEY).asText());
 JsonNode jsonMap = jsonNode.get(MAP_KEY);
 if (jsonMap == null || jsonMap.size() == 0) {
  _map = null;
 } else {
  _map = new HashMap<>();
  Iterator<String> keys = jsonMap.fieldNames();
  while (keys.hasNext()) {
   String key = keys.next();
   _map.put(key, jsonMap.get(key).asText());
  }
 }
}

代码示例来源:origin: decaywood/XueQiuSuperSpider

private List<Stock> processNode(JsonNode node) {
  List<Stock> stocks = new ArrayList<>();
  JsonNode data = node.get("data");
  for (JsonNode jsonNode : data) {
    String symbol = jsonNode.get(0).asText();
    String name = jsonNode.get(1).asText();
    Stock stock = new Stock(name, symbol);
    stocks.add(stock);
  }
  return stocks;
}

代码示例来源:origin: Graylog2/graylog2-server

private static List<ShardRouting> buildShardRoutings(JsonNode shardRoutings) {
  final ImmutableList.Builder<ShardRouting> shardRoutingsBuilder = ImmutableList.builder();
  final Iterator<Map.Entry<String, JsonNode>> it = shardRoutings.fields();
  while (it.hasNext()) {
    final Map.Entry<String, JsonNode> entry = it.next();
    final int shardId = Integer.parseInt(entry.getKey());
    final JsonNode shards = entry.getValue();
      final String state = routing.path("state").asText("unknown").toLowerCase(Locale.ENGLISH);
      final String nodeId = routing.path("node").asText("Unknown");
      final String nodeHostname = null;
      final String relocatingNode = routing.path("relocating_node").asText(null);

代码示例来源:origin: redisson/redisson

@Override
public List<String> findValuesAsText(String fieldName, List<String> foundSoFar)
{
  for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
    if (fieldName.equals(entry.getKey())) {
      if (foundSoFar == null) {
        foundSoFar = new ArrayList<String>();
      }
      foundSoFar.add(entry.getValue().asText());
    } else { // only add children if parent not added
      foundSoFar = entry.getValue().findValuesAsText(fieldName,
        foundSoFar);
    }
  }
  return foundSoFar;
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testUpdateSpan() throws Exception {
  final Span span = tracer.buildSpan("Test#test")
      .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
      .start();
  span.finish();
  elasticsearchClient.waitForCompletion();
  refresh();
  reporter.updateSpan(B3HeaderFormat.getB3Identifiers(tracer, span), null, Collections.singletonMap("foo", "bar"));
  refresh();
  final JsonNode hits = elasticsearchClient.getJson("/stagemonitor-spans*/_search").get("hits");
  assertThat(hits.get("total").intValue()).as(hits.toString()).isEqualTo(1);
  final JsonNode spanJson = hits.get("hits").elements().next().get("_source");
  assertThat(spanJson.get("foo").asText()).as(spanJson.toString()).isEqualTo("bar");
}

相关文章