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

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

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

JsonNode.fieldNames介绍

[英]Method for accessing names of all fields for this Node, iff this node is a JSON Object node.
[中]方法访问此节点的所有字段的名称,如果此节点是JSON对象节点。

代码示例

代码示例来源:origin: joelittlejohn/jsonschema2pojo

private ObjectNode objectSchema(JsonNode exampleObject) {
  ObjectNode schema = this.objectMapper.createObjectNode();
  schema.put("type", "object");
  ObjectNode properties = this.objectMapper.createObjectNode();
  for (Iterator<String> iter = exampleObject.fieldNames(); iter.hasNext();) {
    String property = iter.next();
    properties.set(property, schemaFromExample(exampleObject.get(property)));
  }
  schema.set("properties", properties);
  return schema;
}

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

private void parseProps(JsonNode json) {
 for (Iterator<String> i = json.fieldNames(); i.hasNext();) {
  String p = i.next();                        // add non-reserved as props
  if (!PROTOCOL_RESERVED.contains(p))
   this.addProp(p, json.get(p));
 }
}

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

private void parseMessages(JsonNode json) {
 JsonNode defs = json.get("messages");
 if (defs == null) return;                    // no messages defined
 for (Iterator<String> i = defs.fieldNames(); i.hasNext();) {
  String prop = i.next();
  this.messages.put(prop, parseMessage(prop, defs.get(prop)));
 }
}

代码示例来源:origin: java-json-tools/json-schema-validator

private Map<String, JsonNode> buildDigests(final JsonNode schema)
{
  final ImmutableMap.Builder<String, JsonNode> builder
    = ImmutableMap.builder();
  final Map<String, Digester> map = Maps.newHashMap(digesterMap);
  map.keySet().retainAll(Sets.newHashSet(schema.fieldNames()));
  for (final Map.Entry<String, Digester> entry: map.entrySet())
    builder.put(entry.getKey(), entry.getValue().digest(schema));
  return builder.build();
}

代码示例来源:origin: liferay/liferay-portal

private Map<String, String> _getJsonHomeRootEndpointMap(JsonNode jsonNode) {
  Map<String, String> resourcesMap = new TreeMap<>();
  JsonNode resourcesJsonNode = jsonNode.findPath(
    JSONLDConstants.RESOURCES);
  Iterator<String> fieldNames = resourcesJsonNode.fieldNames();
  while (fieldNames.hasNext()) {
    String fieldName = fieldNames.next();
    JsonNode fieldValue = resourcesJsonNode.get(fieldName);
    if (fieldValue.has(JSONLDConstants.HREF)) {
      JsonNode hrefJsonNode = fieldValue.get(JSONLDConstants.HREF);
      resourcesMap.put(hrefJsonNode.asText(), fieldName);
    }
  }
  return resourcesMap;
}

代码示例来源: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: stackoverflow.com

public static JsonNode merge(JsonNode mainNode, JsonNode updateNode) {

  Iterator<String> fieldNames = updateNode.fieldNames();
  while (fieldNames.hasNext()) {

    String fieldName = fieldNames.next();
    JsonNode jsonNode = mainNode.get(fieldName);
    // if field exists and is an embedded object
    if (jsonNode != null && jsonNode.isObject()) {
      merge(jsonNode, updateNode.get(fieldName));
    }
    else {
      if (mainNode instanceof ObjectNode) {
        // Overwrite field
        JsonNode value = updateNode.get(fieldName);
        ((ObjectNode) mainNode).put(fieldName, value);
      }
    }

  }

  return mainNode;
}

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
  public JsonNode digest(final JsonNode schema)
  {
    // TODO: return an array directly (same for "required" in v4)
    final ObjectNode ret = FACTORY.objectNode();
    final ArrayNode required = FACTORY.arrayNode();
    ret.put("required", required);

    final JsonNode node = schema.get(keyword);
    final List<String> list = Lists.newArrayList(node.fieldNames());

    Collections.sort(list);

    for (final String field: list)
      if (node.get(field).path("required").asBoolean(false))
        required.add(field);

    return ret;
  }
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

@Override
public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
  JAnnotationArrayMember annotationValue = clazz.annotate(JsonPropertyOrder.class).paramArray("value");
  for (Iterator<String> properties = propertiesNode.fieldNames(); properties.hasNext();) {
    annotationValue.param(properties.next());
  }
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

@Override
public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
  JAnnotationArrayMember annotationValue = clazz.annotate(JsonPropertyOrder.class).paramArray("value");
  for (Iterator<String> properties = propertiesNode.fieldNames(); properties.hasNext();) {
    annotationValue.param(properties.next());
  }
}

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

private void resolveObjectToColumns(Map<String, String> columnData, Object value) {
 if (value instanceof PdxInstance) {
  resolvePdxToColumns(columnData, (PdxInstance) value);
 } else if (value instanceof Struct) {
  resolveStructToColumns(columnData, (StructImpl) value);
 } else {
  ObjectMapper mapper = new ObjectMapper();
  JsonNode node = mapper.valueToTree(value);
  node.fieldNames().forEachRemaining(field -> {
   try {
    columnData.put(field, mapper.writeValueAsString(node.get(field)));
   } catch (JsonProcessingException e) {
    columnData.put(field, e.getMessage());
   }
  });
 }
}

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

JsonNode root = objectMapper.readTree(input);
Iterator<String> keysIter = (fieldNames == null ? root.fieldNames() : fieldNames.iterator());

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

public Map<String, Set<String>> getAllMessageFieldsForIndices(final String[] writeIndexWildcards) {
  final String indices = String.join(",", writeIndexWildcards);
  final State request = new State.Builder()
      .indices(indices)
      .withMetadata()
      .build();
  final JestResult jestResult = JestUtils.execute(jestClient, request, () -> "Couldn't read cluster state for indices " + indices);
  final JsonNode indicesJson = getClusterStateIndicesMetadata(jestResult.getJsonObject());
  final ImmutableMap.Builder<String, Set<String>> fields = ImmutableMap.builder();
  final Iterator<Map.Entry<String, JsonNode>> it = indicesJson.fields();
  while (it.hasNext()) {
    final Map.Entry<String, JsonNode> entry = it.next();
    final String indexName = entry.getKey();
    final Set<String> fieldNames = ImmutableSet.copyOf(
        entry.getValue()
            .path("mappings")
            .path(IndexMapping.TYPE_MESSAGE)
            .path("properties").fieldNames()
    );
    if (!fieldNames.isEmpty()) {
      fields.put(indexName, fieldNames);
    }
  }
  return fields.build();
}

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

/**
 * Returns index names and their aliases. This only returns indices which actually have an alias.
 */
@NotNull
public Map<String, Set<String>> getIndexNamesAndAliases(String indexPattern) {
  // only request indices matching the name or pattern in `indexPattern` and only get the alias names for each index,
  // not the settings or mappings
  final GetAliases request = new GetAliases.Builder()
      .addIndex(indexPattern)
      // ES 6 changed the "expand_wildcards" default value for the /_alias API from "open" to "all".
      // Since our code expects only open indices to be returned, we have to explicitly set the parameter now.
      .setParameter("expand_wildcards", "open")
      .build();
  final JestResult jestResult = JestUtils.execute(jestClient, request, () -> "Couldn't collect aliases for index pattern " + indexPattern);
  final ImmutableMap.Builder<String, Set<String>> indexAliasesBuilder = ImmutableMap.builder();
  final Iterator<Map.Entry<String, JsonNode>> it = jestResult.getJsonObject().fields();
  while (it.hasNext()) {
    final Map.Entry<String, JsonNode> entry = it.next();
    final String indexName = entry.getKey();
    final JsonNode aliasMetaData = entry.getValue().path("aliases");
    if (aliasMetaData.isObject()) {
      final ImmutableSet<String> aliasNames = ImmutableSet.copyOf(aliasMetaData.fieldNames());
      indexAliasesBuilder.put(indexName, aliasNames);
    }
  }
  return indexAliasesBuilder.build();
}

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

@Override
  public Object getField(String fieldName) {
    JsonNode fn = jn.get(fieldName);
    if (fn.isContainerNode()) {
      AtomicInteger idx = new AtomicInteger(0);
      List<Field> fields = Lists.newArrayList(fn.fieldNames())
        .stream()
        .map(f -> new Field(f, idx.getAndIncrement()))
        .collect(Collectors.toList());
      return new GenericJsonRecord(fields, fn);
    } else if (fn.isBoolean()) {
      return fn.asBoolean();
    } else if (fn.isInt()) {
      return fn.asInt();
    } else if (fn.isFloatingPointNumber()) {
      return fn.asDouble();
    } else if (fn.isDouble()) {
      return fn.asDouble();
    } else {
      return fn.asText();
    }
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
public void validate(final Processor<FullData, FullData> processor,
  final ProcessingReport report, final MessageBundle bundle,
  final FullData data)
  throws ProcessingException
{
  final Set<String> set = Sets.newLinkedHashSet(required);
  set.removeAll(Sets.newHashSet(data.getInstance().getNode()
    .fieldNames()));
  if (!set.isEmpty())
    report.error(newMsg(data, bundle, "err.common.object.missingMembers")
      .put("required", required)
      .putArgument("missing", toArrayNode(set)));
}

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
public void validate(final Processor<FullData, FullData> processor,
  final ProcessingReport report, final MessageBundle bundle,
  final FullData data)
  throws ProcessingException
{
  final Set<String> set = Sets.newLinkedHashSet(required);
  set.removeAll(Sets.newHashSet(data.getInstance().getNode()
    .fieldNames()));
  if (!set.isEmpty())
    report.error(newMsg(data, bundle, "err.common.object.missingMembers")
      .put("required", required)
      .putArgument("missing", toArrayNode(set)));
}

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

private IndexerOverview getIndexerOverview(IndexSet indexSet) throws TooManyAliasesException {
  final String indexSetId = indexSet.getConfig().id();
  final DeflectorSummary deflectorSummary = deflectorResource.deflector(indexSetId);
  final List<IndexRangeSummary> indexRanges = indexRangesResource.list().ranges();
  final JsonNode indexStats = indices.getIndexStats(indexSet);
  final List<String> indexNames = new ArrayList<>();
  indexStats.fieldNames().forEachRemaining(indexNames::add);
  final Map<String, Boolean> areReopened = indices.areReopened(indexNames);
  final Map<String, IndexSummary> indicesSummaries = buildIndexSummaries(deflectorSummary, indexSet, indexRanges, indexStats, areReopened);
  return IndexerOverview.create(deflectorSummary,
      IndexerClusterOverview.create(indexerClusterResource.clusterHealth(), indexerClusterResource.clusterName().name()),
      countResource.total(indexSetId), indicesSummaries);
}

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

/**
 * Check if a given name is an existing alias.
 *
 * @param alias Name of the alias to check presence for.
 * @return {@code true} if alias is an existing alias, {@code false} if it is non-existing or an index.
 */
public boolean aliasExists(String alias) {
  try {
    final JestResult result = jestClient.execute(new GetSettings.Builder().addIndex(alias).build());
    return result.isSucceeded() && !Iterators.contains(result.getJsonObject().fieldNames(), alias);
  } catch (IOException e) {
    throw new ElasticsearchException("Couldn't check existence of alias " + alias, e);
  }
}

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

/**
 * Check if a given name is an existing index.
 *
 * @param indexName Name of the index to check presence for.
 * @return {@code true} if indexName is an existing index, {@code false} if it is non-existing or an alias.
 */
public boolean exists(String indexName) {
  try {
    final JestResult result = jestClient.execute(new GetSettings.Builder().addIndex(indexName).build());
    return result.isSucceeded() && Iterators.contains(result.getJsonObject().fieldNames(), indexName);
  } catch (IOException e) {
    throw new ElasticsearchException("Couldn't check existence of index " + indexName, e);
  }
}

相关文章