Search in sources :

Example 1 with JSONValue

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

the class JSONHierarchicalSymbolService method getSymbols.

private void getSymbols(EObject value, Consumer<? super DocumentSymbol> acceptor, CancelIndicator ci) {
    operationCanceledManager.checkCanceled(ci);
    if (value instanceof NameValuePair) {
        NameValuePair nameValuePair = (NameValuePair) value;
        DocumentSymbol symbol = symbolMapper.toDocumentSymbol(nameValuePair);
        if (isValid(symbol)) {
            acceptor.accept(symbol);
            JSONValue pairValue = ((NameValuePair) value).getValue();
            if (pairValue != null && pairValue.isContainer()) {
                List<DocumentSymbol> children = symbol.getChildren();
                getSymbols(pairValue, children::add, ci);
            }
        }
    } else if (value instanceof JSONValue) {
        JSONValue casted = (JSONValue) value;
        if (casted.isContainer()) {
            for (EObject child : ((JSONValue) value).getChildren()) {
                getSymbols(child, acceptor, ci);
            }
        } else {
            DocumentSymbol symbol = symbolMapper.toDocumentSymbol(casted);
            if (isValid(symbol)) {
                acceptor.accept(symbol);
            }
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) EObject(org.eclipse.emf.ecore.EObject) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol)

Example 2 with JSONValue

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

the class JSONModelUtils method getPath.

/**
 * Returns the {@link JSONValue} that can be found under the given property path starting from the given
 * {@code object}.
 *
 * Returns an absent {@link Optional} in case the path cannot be resolved (e.g. non-existing properties or values of
 * non-object type).
 *
 * @throws JSONPropertyPathException
 *             if the given path cannot be resolve on {@code object}.
 */
public static Optional<JSONValue> getPath(JSONObject object, List<String> path) {
    if (path.isEmpty()) {
        return Optional.empty();
    }
    final String currentProperty = path.get(0);
    final JSONValue propertyValue = getProperty(object, currentProperty).orElse(null);
    // check that the current property can be resolved
    if (propertyValue == null) {
        return Optional.empty();
    }
    // in case of the last segment
    if (path.size() == 1) {
        // simply return the value
        return Optional.ofNullable(propertyValue);
    }
    // otherwise, check that the property resolves to an JSONObject
    if (!(propertyValue instanceof JSONObject)) {
        return Optional.empty();
    }
    final JSONObject targetObject = (JSONObject) propertyValue;
    // recursively get sub-path of path on targetObject
    return getPath(targetObject, path.subList(1, path.size()));
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 3 with JSONValue

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

the class JSONModelUtils method merge.

/**
 * Moves or copies all {@link NameValuePair}s from object 'source' to object 'target', replacing any
 * {@code NameValuePair}s of same name present in 'target'. The order of properties is preserved.
 *
 * @param target
 *            target object; will be changed in place.
 * @param source
 *            source object; won't be changed iff 'copy' is set to <code>true</code>.
 * @param copy
 *            tells if {@link NameValuePair}s should be copied over, instead of being moved.
 * @param recursive
 *            tells if a recursive merge is to be performed in case an object value in 'target' is overwritten by an
 *            object value in 'source' (i.e. in case of nested objects on both sides).
 */
public static void merge(JSONObject target, JSONObject source, boolean copy, boolean recursive) {
    final Map<String, NameValuePair> targetPairsPerName = JSONModelUtils.getPropertiesAsMap(target, true);
    for (NameValuePair sourcePair : source.getNameValuePairs()) {
        final String sourcePairName = sourcePair.getName();
        final JSONValue sourcePairValue = sourcePair.getValue();
        if (recursive && sourcePairValue instanceof JSONObject) {
            final NameValuePair targetPair = targetPairsPerName.get(sourcePairName);
            final JSONValue targetPairValue = targetPair != null ? targetPair.getValue() : null;
            if (targetPairValue instanceof JSONObject) {
                merge((JSONObject) targetPairValue, (JSONObject) sourcePairValue, copy, recursive);
                continue;
            }
        }
        targetPairsPerName.put(sourcePairName, copy ? EcoreUtil.copy(sourcePair) : sourcePair);
    }
    final List<NameValuePair> targetList = target.getNameValuePairs();
    targetList.clear();
    targetList.addAll(targetPairsPerName.values());
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 4 with JSONValue

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

the class PackageJsonUtils method searchNameValuePair.

@SuppressWarnings("unchecked")
private static <T extends JSONValue> void searchNameValuePair(NameValuePair valuePair, String[] pathElems, int i, Class<T> clazz, List<T> result) {
    String searchName = pathElems[i];
    String jsonName = valuePair.getName();
    JSONValue jsonValue = valuePair.getValue();
    if (i >= pathElems.length || !searchName.equals(jsonName)) {
        return;
    }
    if (i == pathElems.length - 1) {
        if (clazz == null || clazz.isAssignableFrom(jsonValue.getClass())) {
            result.add((T) jsonValue);
            return;
        }
    }
    if (jsonValue instanceof JSONObject) {
        JSONObject jObj = (JSONObject) jsonValue;
        for (NameValuePair child : jObj.getNameValuePairs()) {
            searchNameValuePair(child, pathElems, i + 1, clazz, result);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 5 with JSONValue

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

the class PackageJsonHelper method convertToProjectDescription.

/**
 * Transform the given {@code packageJSON} into an equivalent {@link ProjectDescriptionBuilder} instance. If no
 * further adjustments are required, the client can immediately invoke the {@code #build()} method to obtain the
 * corresponding {@link ProjectDescription}.
 * <p>
 * Note: this method does not implement the package.json feature that a "main" path may point to a folder and then a
 * file "index.js" in that folder will be used as main module (for details see
 * {@link ProjectDescriptionUtils#convertMainPathToModuleSpecifier(String, List)}).
 *
 * @param packageJSON
 *            the JSON document to convert (should be the representation of a valid {@code package.json} file).
 * @param applyDefaultValues
 *            whether default values should be applied to the project description after conversion.
 * @param defaultProjectName
 *            the default project ID (will be ignored if {@code applyDefaultValues} is set to <code>false</code>.
 * @return the project description converted from the given JSON document or <code>null</code> if the root value of
 *         the given JSON document is not a {@link JSONObject}.
 */
public ProjectDescriptionBuilder convertToProjectDescription(JSONDocument packageJSON, boolean applyDefaultValues, String defaultProjectName) {
    JSONValue rootValue = packageJSON.getContent();
    if (!(rootValue instanceof JSONObject)) {
        return null;
    }
    ProjectDescriptionBuilder target = new ProjectDescriptionBuilder();
    List<NameValuePair> rootPairs = ((JSONObject) rootValue).getNameValuePairs();
    convertRootPairs(target, rootPairs);
    String valueOfPropMain = asNonEmptyStringOrNull(getProperty((JSONObject) rootValue, MAIN.name).orElse(null));
    adjustProjectDescriptionAfterConversion(target, applyDefaultValues, defaultProjectName, valueOfPropMain);
    return target;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) ProjectDescriptionBuilder(org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)

Aggregations

JSONValue (org.eclipse.n4js.json.JSON.JSONValue)27 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)19 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)17 EObject (org.eclipse.emf.ecore.EObject)7 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)7 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 URI (org.eclipse.emf.common.util.URI)4 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)3 Multimap (com.google.common.collect.Multimap)3 Inject (com.google.inject.Inject)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 EClass (org.eclipse.emf.ecore.EClass)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 N4JSGlobals (org.eclipse.n4js.N4JSGlobals)3 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)3