Search in sources :

Example 1 with JSONArray

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

the class JSONModelUtils method createArray.

/**
 * Creates a new {@link JSONArray} with the given {@code values} as elements.
 */
public static JSONArray createArray(Collection<JSONValue> values) {
    JSONArray result = JSONFactory.eINSTANCE.createJSONArray();
    result.getElements().addAll(values);
    return result;
}
Also used : JSONArray(org.eclipse.n4js.json.JSON.JSONArray)

Example 2 with JSONArray

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

the class PackageJsonValidatorExtension method checkImplementationId.

/**
 * Validates basic properties of the {@code n4js.implementationId}.
 */
@CheckProperty(property = IMPLEMENTATION_ID)
public void checkImplementationId(JSONValue value) {
    final JSONArray implementedProjectsValue = getSingleDocumentValue(IMPLEMENTED_PROJECTS, JSONArray.class);
    // check basic constraints
    if (!checkIsType(value, JSONPackage.Literals.JSON_STRING_LITERAL, "as implementation ID")) {
        return;
    }
    if (!checkIsNonEmptyString((JSONStringLiteral) value, IMPLEMENTATION_ID)) {
        return;
    }
    final JSONStringLiteral implementationId = (JSONStringLiteral) value;
    // at this point we may assume that the implementationId was set
    if ((implementedProjectsValue == null || implementedProjectsValue.getElements().isEmpty())) {
        // missing implemented projects
        addIssue(IssueCodes.getMessageForPKGJ_APIIMPL_MISSING_IMPL_PROJECTS(), implementationId.eContainer(), JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_APIIMPL_MISSING_IMPL_PROJECTS);
    }
}
Also used : JSONArray(org.eclipse.n4js.json.JSON.JSONArray) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Example 3 with JSONArray

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

the class JSONModelUtils method createStringArray.

/**
 * Creates a new {@link JSONArray} with the given string {@code values} as elements.
 *
 * See {@link #createArray(Collection)} and {@link #createStringLiteral(String)}.
 */
public static JSONArray createStringArray(Iterable<String> values) {
    JSONArray result = JSONFactory.eINSTANCE.createJSONArray();
    values.forEach(v -> result.getElements().add(createStringLiteral(v)));
    return result;
}
Also used : JSONArray(org.eclipse.n4js.json.JSON.JSONArray)

Example 4 with JSONArray

use of org.eclipse.n4js.json.JSON.JSONArray 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 5 with JSONArray

use of org.eclipse.n4js.json.JSON.JSONArray 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)

Aggregations

JSONArray (org.eclipse.n4js.json.JSON.JSONArray)8 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)6 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)5 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)3 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)3 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)3 Optional (com.google.common.base.Optional)2 HashMultimap (com.google.common.collect.HashMultimap)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 Multimap (com.google.common.collect.Multimap)2 Inject (com.google.inject.Inject)2 Singleton (com.google.inject.Singleton)2 File (java.io.File)2 InvalidPathException (java.nio.file.InvalidPathException)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2