/** * Modifies an existing node value * * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public JsonNode modify(Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }
/** * Modifies an existing node value * * @param root The root node * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public static JsonNode modify(JsonNode root, Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(root, parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }
/** * Modifies an existing node value * * @param root The root node * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public static JsonNode modify(JsonNode root, Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(root, parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }