use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class PackageJsonValidatorExtension method checkIsDependenciesSection.
/**
* Checks whether the given {@code sectionValue} is a structurally valid package.json dependency section (including
* the version constraints).
*/
private void checkIsDependenciesSection(JSONValue sectionValue) {
if (!checkIsType(sectionValue, JSONPackage.Literals.JSON_OBJECT, "as list of dependencies")) {
return;
}
final JSONObject dependenciesObject = (JSONObject) sectionValue;
for (NameValuePair entry : dependenciesObject.getNameValuePairs()) {
final JSONValue versionRequirement = entry.getValue();
if (checkIsType(versionRequirement, JSONPackage.Literals.JSON_STRING_LITERAL, "as version specifier")) {
JSONStringLiteral jsonStringVersionRequirement = (JSONStringLiteral) versionRequirement;
String constraintValue = jsonStringVersionRequirement.getValue();
validateSemver(jsonStringVersionRequirement, constraintValue);
}
}
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class JSONCodeActionService method createInstallNpmCommand.
private Command createInstallNpmCommand(Options options, Diagnostic diag) {
EObject element = getEObject(options, diag);
if (element instanceof NameValuePair) {
NameValuePair pair = (NameValuePair) element;
String projectName = pair.getName();
String version = JSONModelUtils.asStringOrNull(pair.getValue());
return new Command("Install npm package to workspace", INSTALL_NPM, Arrays.asList(projectName, version, options.getCodeActionParams().getTextDocument().getUri()));
}
return null;
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class JSONModelUtils method setPath.
private static <V extends JSONValue> V setPath(JSONObject object, List<String> currentPath, List<String> fullPath, V value) {
if (currentPath.size() == 0) {
return null;
}
final String currentProperty = currentPath.get(0);
final int pathLength = currentPath.size();
// if we are at the end of the path
if (pathLength == 1) {
// set the value on 'object'
setProperty(object, currentProperty, value);
return value;
}
// obtain NameValuePair that matches the first segment in propertyPath
final Optional<NameValuePair> pair = getNameValuePair(object, currentProperty);
// if pair already exists
if (pair.isPresent()) {
final JSONValue pathValue = pair.get().getValue();
// check whether the value is an object
if (!(pathValue instanceof JSONObject)) {
// if not, the property path is invalid
throw new JSONPropertyPathException("Cannot resolve JSON property path further then " + fullPath.subList(0, fullPath.size() - pathLength).stream().collect(Collectors.joining(".")) + ". " + pathValue + " is not a JSONObject.", null);
}
// setPath recursively on the (object) value of the existing pair
return setPath((JSONObject) pathValue, currentPath.subList(1, pathLength), fullPath, value);
} else {
// add new object name-value-pair for current property
final JSONObject nextObject = addProperty(object, currentProperty, JSONFactory.eINSTANCE.createJSONObject());
return setPath(nextObject, currentPath.subList(1, pathLength), fullPath, value);
}
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class JSONModelUtils method createNameValuePair.
/**
* Creates a new {@link NameValuePair}.
*/
public static NameValuePair createNameValuePair(String name, JSONValue value) {
NameValuePair result = JSONFactory.eINSTANCE.createNameValuePair();
result.setName(name);
result.setValue(value);
return result;
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class JSONModelUtils method addProperty.
/**
* Adds a new {@link NameValuePair} to the {@code object}, with given {@code name} and {@code value}.
*
* Does not check {@code object} for duplicate {@link NameValuePair} with the same name.
*
* @returns The newly set value.
*/
public static <V extends JSONValue> V addProperty(JSONObject object, String name, V value) {
final NameValuePair nameValuePair = JSONFactory.eINSTANCE.createNameValuePair();
nameValuePair.setName(name);
nameValuePair.setValue(value);
object.getNameValuePairs().add(nameValuePair);
return value;
}
Aggregations