use of org.eclipse.n4js.json.JSON.JSONObject 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.JSONObject in project n4js by eclipse.
the class JSONModelUtils method merge.
/**
* Like {@link #merge(JSONObject, JSONObject, boolean, boolean)}, but for {@link JSONDocument}.
*/
public static void merge(JSONDocument target, JSONDocument source, boolean copy, boolean recursive) {
JSONValue targetContent = target.getContent();
JSONValue sourceContent = source.getContent();
if (sourceContent instanceof JSONObject && targetContent instanceof JSONObject) {
merge((JSONObject) targetContent, (JSONObject) sourceContent, copy, recursive);
} else {
target.setContent(copy ? EcoreUtil.copy(sourceContent) : sourceContent);
}
}
use of org.eclipse.n4js.json.JSON.JSONObject 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.JSONObject in project n4js by eclipse.
the class JSONSemanticSequencer method sequence.
@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
EPackage epackage = semanticObject.eClass().getEPackage();
ParserRule rule = context.getParserRule();
Action action = context.getAssignedAction();
Set<Parameter> parameters = context.getEnabledBooleanParameters();
if (epackage == JSONPackage.eINSTANCE)
switch(semanticObject.eClass().getClassifierID()) {
case JSONPackage.JSON_ARRAY:
sequence_JSONArray(context, (JSONArray) semanticObject);
return;
case JSONPackage.JSON_BOOLEAN_LITERAL:
sequence_JSONBooleanLiteral(context, (JSONBooleanLiteral) semanticObject);
return;
case JSONPackage.JSON_DOCUMENT:
sequence_JSONDocument(context, (JSONDocument) semanticObject);
return;
case JSONPackage.JSON_NULL_LITERAL:
sequence_JSONNullLiteral(context, (JSONNullLiteral) semanticObject);
return;
case JSONPackage.JSON_NUMERIC_LITERAL:
sequence_JSONNumericLiteral(context, (JSONNumericLiteral) semanticObject);
return;
case JSONPackage.JSON_OBJECT:
sequence_JSONObject(context, (JSONObject) semanticObject);
return;
case JSONPackage.JSON_STRING_LITERAL:
sequence_JSONStringLiteral(context, (JSONStringLiteral) semanticObject);
return;
case JSONPackage.NAME_VALUE_PAIR:
sequence_NameValuePair(context, (NameValuePair) semanticObject);
return;
}
if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
use of org.eclipse.n4js.json.JSON.JSONObject in project n4js by eclipse.
the class PackageJsonUtils method findNameValuePairs.
/**
* Traverses the given {@link Resource} and finds all {@link JSONValue}s that match the given
* {@link PackageJsonProperties}. The list of results is filtered in case a {@link Class} is given.
*
* @return all {@link NameValuePair}s for the given {@link PackageJsonProperties} in the given json resource
*/
public static <T extends JSONValue> List<T> findNameValuePairs(Resource jsonResource, PackageJsonProperties prop, Class<T> clazz) {
String[] pathElements = prop.getPathElements();
List<T> nameValuePairs = new LinkedList<>();
EList<EObject> contents = jsonResource.getContents();
EObject rootElem = contents.get(0);
if (rootElem instanceof JSONDocument) {
JSONDocument jsonDocument = (JSONDocument) rootElem;
JSONValue jsonContent = jsonDocument.getContent();
if (jsonContent instanceof JSONObject) {
JSONObject jsonObj = (JSONObject) jsonContent;
for (NameValuePair child : jsonObj.getNameValuePairs()) {
searchNameValuePair(child, pathElements, 0, clazz, nameValuePairs);
}
}
}
return nameValuePairs;
}
Aggregations