public static boolean isValueExists(IStructuredDocument document, IJSONPath path) { IJSONModel model = null; try { model = (IJSONModel) StructuredModelManager.getModelManager().getModelForRead(document); IJSONPair pair = findByPath(model.getDocument(), path.getSegments()); return pair != null; } finally { if (model != null) { model.releaseFromRead(); } } }
public static void removePath(IStructuredDocument document, IJSONPath path) { IJSONModel model = null; try { model = (IJSONModel) StructuredModelManager.getModelManager().getModelForRead(document); IJSONPair pair = findByPath(model.getDocument(), path.getSegments()); if (pair != null) { document.replaceText( document, pair.getStartOffset(), pair.getEndOffset() - pair.getStartOffset(), ""); } } finally { if (model != null) { model.releaseFromRead(); } } }
public static Object getValue(IStructuredDocument document, IJSONPath path) { IJSONModel model = null; try { model = (IJSONModel) StructuredModelManager.getModelManager().getModelForRead(document); IJSONPair pair = findByPath(model.getDocument(), path.getSegments()); if (pair != null) { IJSONValue value = pair.getValue(); return getValue(value); } } finally { if (model != null) { model.releaseFromRead(); } } return null; }
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(); } } }