com.fasterxml.jackson.databind.node.ArrayNode.set()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(140)

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

ArrayNode.set介绍

[英]Method that will set specified field, replacing old value, if any.
[中]方法,该方法将设置指定的字段,并替换旧值(如果有)。

代码示例

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

@Override
void writeChild(String childName, JsonNode childNode) {
  int index = toIndex(childName);
  // allow replacing elements at index
  if (index < node.size()) {
    node.set(index, childNode);
  }
  // allow appending elements to the end of the array...
  else if (index == node.size()) {
    node.add(childNode);
  } else {
    throw new ArrayIndexOutOfBoundsException("Array index out of bounds: " + index + ". Size: " + node.size());
  }
}

代码示例来源:origin: json-path/JsonPath

@Override
public void setArrayIndex(Object array, int index, Object newValue) {
  if (!isArray(array)) {
    throw new UnsupportedOperationException();
  } else {
    ArrayNode arrayNode = toJsonArray(array);
    if (index == arrayNode.size()){
      arrayNode.add(createJsonElement(newValue));
    }else {
      arrayNode.set(index, createJsonElement(newValue));
    }
  }
}

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

} else {
  final ArrayNode array = (ArrayNode) child;
  array.set(index, TextNode.valueOf(value));
  return;

代码示例来源:origin: json-path/JsonPath

@Override
public void setProperty(Object obj, Object key, Object value) {
  // jlolling: Bug: #211 avoid create cloned nodes
  if (isMap(obj)) {
    setValueInObjectNode((ObjectNode) obj, key, value);
  } else {
    ArrayNode array = (ArrayNode) obj;
    int index;
    if (key != null) {
      index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
    } else {
      index = array.size();
    }
    if (index == array.size()) {
      array.add(createJsonElement(value));
    } else {
      array.set(index, createJsonElement(value));
    }
  }
}

代码示例来源:origin: com.jayway.jsonpath/json-path

@Override
public void setArrayIndex(Object array, int index, Object newValue) {
  if (!isArray(array)) {
    throw new UnsupportedOperationException();
  } else {
    ArrayNode arrayNode = toJsonArray(array);
    if (index == arrayNode.size()){
      arrayNode.add(createJsonElement(newValue));
    }else {
      arrayNode.set(index, createJsonElement(newValue));
    }
  }
}

代码示例来源:origin: com.jayway.jsonpath/json-path

@Override
public void setProperty(Object obj, Object key, Object value) {
  // jlolling: Bug: #211 avoid create cloned nodes
  if (isMap(obj)) {
    setValueInObjectNode((ObjectNode) obj, key, value);
  } else {
    ArrayNode array = (ArrayNode) obj;
    int index;
    if (key != null) {
      index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
    } else {
      index = array.size();
    }
    if (index == array.size()) {
      array.add(createJsonElement(value));
    } else {
      array.set(index, createJsonElement(value));
    }
  }
}

代码示例来源:origin: HotelsDotCom/styx

@Override
public void setChild(JsonNode parent, JsonNode child) {
  ((ArrayNode) parent).set(index, child);
}

代码示例来源:origin: org.keycloak/keycloak-admin-cli

private static void setArrayItem(ArrayNode list, int index, JsonNode valNode) {
  if (index == -1) {
    // append to end of array
    list.add(valNode);
    return;
  }
  // make sure items up to index exist
  for (int i = list.size(); i < index+1; i++) {
    list.add(NullNode.instance);
  }
  list.set(index, valNode);
}

代码示例来源:origin: algesten/jsondiff

@Override
public void set(int index, JzonElement el) {
  wrapped.set(index, (JsonNode) el.unwrap());
}

代码示例来源:origin: com.github.foodev/jsondiff

@Override
public void set(int index, JzonElement el) {
  wrapped.set(index, (JsonNode) el.unwrap());
}

代码示例来源:origin: io.projectreactor/reactor-bus

@Override
public void setProperty(Object obj, Object key, Object value) {
  if (obj instanceof Map) {
    ((Map) obj).put(key, value);
  } else if (obj instanceof List) {
    int idx = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
    ((List) obj).add(idx, value);
  } else if (obj instanceof ObjectNode) {
    ((ObjectNode) obj).set(key.toString(), (JsonNode) value);
  } else if (obj instanceof ArrayNode) {
    int idx = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
    ((ArrayNode) obj).set(idx, (JsonNode) value);
  } else {
    setProperty(mapper.convertValue(obj, JsonNode.class), key, value);
  }
}

代码示例来源:origin: org.projectreactor/reactor-core

@Override
public void setProperty(Object obj, Object key, Object value) {
  if(obj instanceof Map) {
    ((Map)obj).put(key, value);
  } else if(obj instanceof List) {
    int idx = key instanceof Integer ? (Integer)key : Integer.parseInt(key.toString());
    ((List)obj).add(idx, value);
  } else if(obj instanceof ObjectNode) {
    ((ObjectNode)obj).set(key.toString(), (JsonNode)value);
  } else if(obj instanceof ArrayNode) {
    int idx = key instanceof Integer ? (Integer)key : Integer.parseInt(key.toString());
    ((ArrayNode)obj).set(idx, (JsonNode)value);
  } else {
    setProperty(mapper.convertValue(obj, JsonNode.class), key, value);
  }
}

代码示例来源:origin: com.reprezen.genflow/genflow-api

/**
   * If this is an array node, handle any refs among its element values
   */
  private void assembleArrayElements(JsonNode node, Reference base, Set<Reference> visited) {
    if (node instanceof ArrayNode) {
      ArrayNode arrayNode = (ArrayNode) node;
      for (int i = 0; i < node.size(); i++) {
        arrayNode.set(i, assembleNode(arrayNode.get(i), base, visited));
      }
    }
  }
}

代码示例来源:origin: com.reprezen.genflow/genflow-api

private void maybeInlineArrayElements(JsonNode node, Reference base) {
  if (node instanceof ArrayNode) {
    ArrayNode arrayNode = (ArrayNode) node;
    for (int i = 0; i < arrayNode.size(); i++) {
      arrayNode.set(i, maybeInline(arrayNode.get(i), base));
    }
  }
}

代码示例来源:origin: io.micronaut/runtime

private JsonNode getOrCreateNodeAtIndex(JsonNodeFactory nodeFactory, ArrayNode arrayNode, int arrayIndex) {
  JsonNode jsonNode = arrayNode.get(arrayIndex);
  if (jsonNode == null || !(jsonNode instanceof ObjectNode)) {
    jsonNode = new ObjectNode(nodeFactory);
    arrayNode.set(arrayIndex, jsonNode);
  }
  return jsonNode;
}

代码示例来源:origin: com.github.lafa.jsonpath/json-path

@Override
public void setArrayIndex(Object array, int index, Object newValue) {
  if (!isArray(array)) {
    throw new UnsupportedOperationException();
  } else {
    ArrayNode arrayNode = toJsonArray(array);
    if (index == arrayNode.size()){
      arrayNode.add(createJsonElement(newValue));
    }else {
      arrayNode.set(index, createJsonElement(newValue));
    }
  }
}

代码示例来源:origin: flipkart-incubator/zjsonpatch

@Override
public void replace(List<String> path, JsonNode value) {
  if (path.isEmpty()) {
    error(Operation.REPLACE, "path is empty");
  } else {
    JsonNode parentNode = getParentNode(path, Operation.REPLACE);
    String fieldToReplace = path.get(path.size() - 1).replaceAll("\"", "");
    if (isNullOrEmpty(fieldToReplace) && path.size() == 1)
      target = value;
    else if (parentNode.isObject() && parentNode.has(fieldToReplace)) {
      ((ObjectNode) parentNode).replace(fieldToReplace, value);
    } else if (parentNode.isArray())
      ((ArrayNode) parentNode).set(arrayIndex(fieldToReplace, parentNode.size() - 1, false), value);
    else
      error(Operation.REPLACE, "noSuchPath in source, path provided : " + PathUtils.getPathRepresentation(path));
  }
}

代码示例来源:origin: com.flipkart.zjsonpatch/zjsonpatch

@Override
public void replace(List<String> path, JsonNode value) {
  if (path.isEmpty()) {
    error(Operation.REPLACE, "path is empty");
  } else {
    JsonNode parentNode = getParentNode(path, Operation.REPLACE);
    String fieldToReplace = path.get(path.size() - 1).replaceAll("\"", "");
    if (isNullOrEmpty(fieldToReplace) && path.size() == 1)
      target = value;
    else if (parentNode.isObject() && parentNode.has(fieldToReplace)) {
      ((ObjectNode) parentNode).replace(fieldToReplace, value);
    } else if (parentNode.isArray())
      ((ArrayNode) parentNode).set(arrayIndex(fieldToReplace, parentNode.size() - 1, false), value);
    else
      error(Operation.REPLACE, "noSuchPath in source, path provided : " + PathUtils.getPathRepresentation(path));
  }
}

代码示例来源:origin: line/centraldogma

@Override
  JsonNode apply(final JsonNode node) {
    ensureExistence(node);

    final JsonNode replacement = valueCopy();
    if (path.toString().isEmpty()) {
      return replacement;
    }
    final JsonNode parent = node.at(path.head());
    final String rawToken = path.last().getMatchingProperty();
    if (parent.isObject()) {
      ((ObjectNode) parent).set(rawToken, replacement);
    } else {
      ((ArrayNode) parent).set(Integer.parseInt(rawToken), replacement);
    }
    return node;
  }
}

代码示例来源:origin: com.linecorp.centraldogma/centraldogma-common

@Override
  JsonNode apply(final JsonNode node) {
    ensureExistence(node);

    final JsonNode replacement = valueCopy();
    if (path.toString().isEmpty()) {
      return replacement;
    }
    final JsonNode parent = node.at(path.head());
    final String rawToken = path.last().getMatchingProperty();
    if (parent.isObject()) {
      ((ObjectNode) parent).set(rawToken, replacement);
    } else {
      ((ArrayNode) parent).set(Integer.parseInt(rawToken), replacement);
    }
    return node;
  }
}

相关文章