private static int getEndOffset(IJSONNode parent, boolean inside) { if (parent == null) { return 0; } switch (parent.getNodeType()) { case IJSONNode.OBJECT_NODE: if (parent.hasChildNodes()) { IJSONNode lastChild = parent.getLastChild(); boolean childInside = inside && (isSimpleValue(lastChild) || (lastChild.getNodeType() == IJSONNode.PAIR_NODE && isSimpleValue(((IJSONPair) lastChild).getValue()))); return getEndOffset(lastChild, childInside); } return parent.getStartOffset() + (inside ? 1 : 0); case IJSONNode.PAIR_NODE: if (!inside) { return parent.getEndOffset(); } IJSONPair pair = (IJSONPair) parent; IJSONValue value = (IJSONValue) pair.getValue(); if (value != null) { return getEndOffset(value, false); } return pair.getEndOffset(); case IJSONNode.VALUE_BOOLEAN_NODE: case IJSONNode.VALUE_NULL_NODE: case IJSONNode.VALUE_NUMBER_NODE: case IJSONNode.VALUE_STRING_NODE: return parent.getStartOffset() + parent.getFirstStructuredDocumentRegion().getFirstRegion().getLength(); default: return parent.getEndOffset(); } }
public static void setValue(IStructuredDocument document, IJSONPath path, Object value) { IJSONModel model = null; try { model = (IJSONModel) StructuredModelManager.getModelManager().getModelForEdit(document); IJSONNode parent = model.getDocument().getFirstChild(); String[] segments = path.getSegments(); String name = null; int replaceOffset = 0; int replaceLength = 0; boolean isArray = false; int startIndex = NO_START_INDEX; StringBuilder newContent = new StringBuilder(); for (int i = 0; i < segments.length; i++) { name = segments[i]; isArray = isArray(path, i); IJSONPair node = findByPath(parent, name); if (node != null) { parent = node; IJSONNode jsonValue = node.getValue(); if (isObject(jsonValue)) { parent = jsonValue; replaceOffset = getEndOffset(parent, true); } else { if (jsonValue != null) { replaceOffset = jsonValue.getStartOffset(); replaceLength = isArray(jsonValue) ? jsonValue.getEndStructuredDocumentRegion().getEndOffset() - jsonValue.getFirstStructuredDocumentRegion().getStartOffset() : jsonValue.getFirstStructuredDocumentRegion().getFirstRegion().getLength(); } } } else { if (isObjectOrArray(parent)) { replaceOffset = getEndOffset(parent, true); if (parent.hasChildNodes()) { newContent.append(","); } } else { newContent.append(isArray ? "[" : "{"); if (startIndex == NO_START_INDEX) { startIndex = i - 1; } } newLineAndIndent(i, newContent); newContent.append("\""); newContent.append(name); newContent.append("\": "); parent = null; } } addValue(value, newContent); if (startIndex != NO_START_INDEX) { // close JSON object or Array for (int i = segments.length - 1; i > startIndex; i--) { if (i == -1) { newLineAndIndent(0, newContent); newContent.append("}"); } else { name = segments[i]; isArray = isArray(path, i); newLineAndIndent(i - 1, newContent); newContent.append(isArray ? "]" : "}"); } } } document.replaceText(document, replaceOffset, replaceLength, newContent.toString()); } finally { if (model != null) { model.releaseFromEdit(); } } }