Search in sources :

Example 1 with JSONObject

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

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

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

the class JSONModelUtils method getPathToNameValuePairOrNull.

/**
 * Returns the path to the given {@link NameValuePair} in its containing JSON object tree or <code>null</code> in
 * case of syntax error.
 */
public static List<String> getPathToNameValuePairOrNull(NameValuePair nvp) {
    List<String> path = new ArrayList<>();
    EObject currNVP = nvp;
    do {
        String currName = ((NameValuePair) currNVP).getName();
        if (currName == null) {
            // syntax error in JSON file
            return null;
        }
        path.add(currName);
        currNVP = currNVP.eContainer();
        currNVP = currNVP instanceof JSONObject ? currNVP.eContainer() : null;
    } while (currNVP instanceof NameValuePair);
    Collections.reverse(path);
    return path;
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList)

Example 4 with JSONObject

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

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

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