Search in sources :

Example 16 with JSONObject

use of org.eclipse.n4js.json.JSON.JSONObject in project n4js by eclipse.

the class ProjectDescriptionLoader method copy.

private JSONValue copy(JsonElement jsonElement) {
    if (jsonElement.isJsonNull()) {
        return JSONFactory.eINSTANCE.createJSONNullLiteral();
    }
    if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive primitive = jsonElement.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            JSONBooleanLiteral result = JSONFactory.eINSTANCE.createJSONBooleanLiteral();
            result.setBooleanValue(primitive.getAsBoolean());
            return result;
        }
        if (primitive.isNumber()) {
            JSONNumericLiteral result = JSONFactory.eINSTANCE.createJSONNumericLiteral();
            result.setValue(primitive.getAsBigDecimal());
            return result;
        }
        if (primitive.isString()) {
            JSONStringLiteral result = JSONFactory.eINSTANCE.createJSONStringLiteral();
            result.setValue(primitive.getAsString());
            return result;
        }
        throw new IllegalArgumentException(jsonElement.toString());
    }
    if (jsonElement.isJsonObject()) {
        JsonObject object = jsonElement.getAsJsonObject();
        JSONObject result = JSONFactory.eINSTANCE.createJSONObject();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            NameValuePair pair = JSONFactory.eINSTANCE.createNameValuePair();
            pair.setName(entry.getKey());
            pair.setValue(copy(entry.getValue()));
            result.getNameValuePairs().add(pair);
        }
        return result;
    }
    if (jsonElement.isJsonArray()) {
        JsonArray array = jsonElement.getAsJsonArray();
        JSONArray result = JSONFactory.eINSTANCE.createJSONArray();
        for (JsonElement arrayElement : array) {
            result.getElements().add(copy(arrayElement));
        }
        return result;
    }
    throw new IllegalArgumentException(jsonElement.toString());
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JsonPrimitive(com.google.gson.JsonPrimitive) JSONNumericLiteral(org.eclipse.n4js.json.JSON.JSONNumericLiteral) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) JsonObject(com.google.gson.JsonObject) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral) JsonArray(com.google.gson.JsonArray) JSONBooleanLiteral(org.eclipse.n4js.json.JSON.JSONBooleanLiteral) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JsonElement(com.google.gson.JsonElement) Map(java.util.Map)

Example 17 with JSONObject

use of org.eclipse.n4js.json.JSON.JSONObject in project n4js by eclipse.

the class PackageJsonValidatorExtension method getModuleFilterInformation.

/**
 * Validates the structure of the given {@code value} as module filter specifier and returns the information that
 * can be extracted from it.
 *
 * Returns the module filter specifier information of {@code value} in terms of a
 * {@link ValidationModuleFilterSpecifier}.
 *
 * Returns {@code null} if the given {@code value} is not a valid representation of a module filter specifier.
 *
 * Similar to {@link PackageJsonUtils#asModuleFilterSpecifierOrNull(JSONValue)} but also validates the structure
 * along the way.
 */
private ValidationModuleFilterSpecifier getModuleFilterInformation(JSONValue value, ModuleFilterType type) {
    // 1st variant:
    if (value instanceof JSONStringLiteral) {
        return new ValidationModuleFilterSpecifier(((JSONStringLiteral) value).getValue(), null, type, value);
    }
    // 2nd variant:
    if (value instanceof JSONObject) {
        final List<NameValuePair> pairs = ((JSONObject) value).getNameValuePairs();
        final NameValuePair sourceContainerPair = pairs.stream().filter(p -> NV_SOURCE_CONTAINER.name.equals(p.getName())).findFirst().orElse(null);
        final NameValuePair moduleFilterPair = pairs.stream().filter(p -> NV_MODULE.name.equals(p.getName())).findFirst().orElse(null);
        // make sure the pairs are of correct type (or null in case of sourceContainerPair)
        if ((sourceContainerPair == null || checkIsType(sourceContainerPair.getValue(), JSONPackage.Literals.JSON_STRING_LITERAL)) && (moduleFilterPair != null && checkIsType(moduleFilterPair.getValue(), JSONPackage.Literals.JSON_STRING_LITERAL))) {
            final String sourceContainer = sourceContainerPair != null ? ((JSONStringLiteral) sourceContainerPair.getValue()).getValue() : null;
            final String moduleFilter = ((JSONStringLiteral) moduleFilterPair.getValue()).getValue();
            return new ValidationModuleFilterSpecifier(moduleFilter, sourceContainer, type, value);
        }
    }
    // otherwise 'value' does not represent a valid module filter specifier
    addIssue(IssueCodes.getMessageForPKGJ_INVALID_MODULE_FILTER_SPECIFIER(), value, IssueCodes.PKGJ_INVALID_MODULE_FILTER_SPECIFIER);
    return null;
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Example 18 with JSONObject

use of org.eclipse.n4js.json.JSON.JSONObject in project n4js by eclipse.

the class PackageJsonValidatorExtension method doGetSourceContainers.

/**
 * Validates the correct structure of a {@link PackageJsonProperties#SOURCES} section and returns a map between the
 * declared source container types and corresponding {@link JSONStringLiteral}s that specify the various source
 * container paths.
 */
private Multimap<SourceContainerType, List<JSONStringLiteral>> doGetSourceContainers() {
    final Collection<JSONValue> sourcesValues = getDocumentValues(SOURCES);
    // first check whether n4js.sources section has been defined at all
    if (sourcesValues.isEmpty()) {
        // return an empty map
        return ImmutableMultimap.<SourceContainerType, List<JSONStringLiteral>>of();
    }
    // first check type of all occuring 'sources' sections
    if (!checkIsType(sourcesValues, JSONPackage.Literals.JSON_OBJECT, "as source container section")) {
        // return an empty map
        return ImmutableMultimap.<SourceContainerType, List<JSONStringLiteral>>of();
    }
    // only consider the first n4js.sources section for further validation (in case of duplicates)
    final JSONValue sourcesValue = sourcesValues.iterator().next();
    final JSONObject sourceContainerObject = (JSONObject) sourcesValue;
    final Multimap<SourceContainerType, List<JSONStringLiteral>> sourceContainerValues = HashMultimap.create();
    for (NameValuePair pair : sourceContainerObject.getNameValuePairs()) {
        final String sourceContainerType = pair.getName();
        // compute type of source container sub-section
        final SourceContainerType containerType = PackageJsonUtils.parseSourceContainerType(pair.getName());
        // check that sourceContainerType represents a valid source container type
        if (containerType == null) {
            addIssue(IssueCodes.getMessageForPKGJ_INVALID_SOURCE_CONTAINER_TYPE(sourceContainerType), pair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_INVALID_SOURCE_CONTAINER_TYPE);
            continue;
        }
        // check type of RHS (list of source paths)
        if (!checkIsType(pair.getValue(), JSONPackage.Literals.JSON_ARRAY, "as source container list")) {
            continue;
        }
        final JSONArray sourceContainerSpecifiers = (JSONArray) pair.getValue();
        // collect all source container paths in this list
        final List<JSONStringLiteral> specifierLiterals = new ArrayList<>();
        for (JSONValue specifier : sourceContainerSpecifiers.getElements()) {
            if (!checkIsType(specifier, JSONPackage.Literals.JSON_STRING_LITERAL, "as source container specifier")) {
                continue;
            }
            specifierLiterals.add((JSONStringLiteral) specifier);
        }
        // This may override a value in case of a duplicate containerType (e.g. two external sections).
        // However, this will also issue an appropriate warning for a duplicate key and
        // is therefore not handled here.
        sourceContainerValues.put(containerType, specifierLiterals);
    }
    return sourceContainerValues;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

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