Search in sources :

Example 11 with JSONObject

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);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Example 12 with JSONObject

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);
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 13 with JSONObject

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);
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 14 with JSONObject

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));
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) Action(org.eclipse.xtext.Action) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) JSONNumericLiteral(org.eclipse.n4js.json.JSON.JSONNumericLiteral) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral) EPackage(org.eclipse.emf.ecore.EPackage) JSONBooleanLiteral(org.eclipse.n4js.json.JSON.JSONBooleanLiteral) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) Parameter(org.eclipse.xtext.Parameter) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) JSONNullLiteral(org.eclipse.n4js.json.JSON.JSONNullLiteral)

Example 15 with JSONObject

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;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) EObject(org.eclipse.emf.ecore.EObject) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) LinkedList(java.util.LinkedList)

Aggregations

JSONObject (org.eclipse.n4js.json.JSON.JSONObject)18 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)16 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)14 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)6 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)3 ArrayList (java.util.ArrayList)2 EObject (org.eclipse.emf.ecore.EObject)2 JSONBooleanLiteral (org.eclipse.n4js.json.JSON.JSONBooleanLiteral)2 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)2 JSONNumericLiteral (org.eclipse.n4js.json.JSON.JSONNumericLiteral)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 URI (org.eclipse.emf.common.util.URI)1 EPackage (org.eclipse.emf.ecore.EPackage)1 JSONNullLiteral (org.eclipse.n4js.json.JSON.JSONNullLiteral)1