@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()); } }
@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)); } } }
} else { final ArrayNode array = (ArrayNode) child; array.set(index, TextNode.valueOf(value)); return;
@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)); } } }
@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)); } } }
@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)); } } }
@Override public void setChild(JsonNode parent, JsonNode child) { ((ArrayNode) parent).set(index, child); }
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); }
@Override public void set(int index, JzonElement el) { wrapped.set(index, (JsonNode) el.unwrap()); }
@Override public void set(int index, JzonElement el) { wrapped.set(index, (JsonNode) el.unwrap()); }
@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); } }
@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); } }
/** * 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)); } } } }
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)); } } }
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; }
@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)); } } }
@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)); } }
@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)); } }
@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; } }
@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; } }