Search in sources :

Example 21 with NameValuePair

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

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

the class JSONValidator method checkDuplicateKeys.

/**
 * Checks for duplicate keys in {@link JSONObject}s.
 */
@Check
public void checkDuplicateKeys(JSONObject object) {
    final Map<String, JSONValue> values = new HashMap<>();
    for (NameValuePair pair : object.getNameValuePairs()) {
        final JSONValue value = values.get(pair.getName());
        if (value != null) {
            final INode duplicatedNode = NodeModelUtils.findActualNodeFor(value);
            final int duplicatedLine = NodeModelUtils.getLineAndColumn(duplicatedNode, duplicatedNode.getOffset()).getLine();
            addIssue(JSONIssueCodes.getMessageForJSON_DUPLICATE_KEY(pair.getName(), duplicatedLine), pair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, JSONIssueCodes.JSON_DUPLICATE_KEY);
        }
        values.put(pair.getName(), pair.getValue());
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) INode(org.eclipse.xtext.nodemodel.INode) HashMap(java.util.HashMap) Check(org.eclipse.xtext.validation.Check)

Example 23 with NameValuePair

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

Example 24 with NameValuePair

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

the class PackageJsonHelper method convertDependencies.

private void convertDependencies(ProjectDescriptionBuilder target, List<NameValuePair> depPairs, boolean avoidDuplicates, DependencyType type) {
    Objects.requireNonNull(type);
    Set<String> existingProjectNames = new HashSet<>();
    if (avoidDuplicates) {
        for (ProjectDependency pd : target.getDependencies()) {
            existingProjectNames.add(pd.getPackageName());
        }
    }
    for (NameValuePair pair : depPairs) {
        String projectName = pair.getName();
        boolean addProjectDependency = true;
        addProjectDependency &= projectName != null && !projectName.isEmpty();
        addProjectDependency &= !(avoidDuplicates && existingProjectNames.contains(projectName));
        existingProjectNames.add(projectName);
        if (addProjectDependency) {
            JSONValue value = pair.getValue();
            String valueStr = asStringOrNull(value);
            NPMVersionRequirement versionRequirement = valueStr != null ? semverHelper.parse(valueStr) : null;
            ProjectDependency dep = new ProjectDependency(projectName, type, valueStr, versionRequirement);
            target.addDependency(dep);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) ProjectDependency(org.eclipse.n4js.packagejson.projectDescription.ProjectDependency) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement) HashSet(java.util.HashSet)

Example 25 with NameValuePair

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

NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)28 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)17 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)17 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)7 EObject (org.eclipse.emf.ecore.EObject)6 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)4 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 URI (org.eclipse.emf.common.util.URI)2 JSONBooleanLiteral (org.eclipse.n4js.json.JSON.JSONBooleanLiteral)2 JSONNumericLiteral (org.eclipse.n4js.json.JSON.JSONNumericLiteral)2 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)2 NPMVersionRequirement (org.eclipse.n4js.semver.Semver.NPMVersionRequirement)2 INode (org.eclipse.xtext.nodemodel.INode)2 Check (org.eclipse.xtext.validation.Check)2 Optional (com.google.common.base.Optional)1